Creating PDF files with PowerShell
Creating new PDF files takes a similar approach to what I have built for PSWriteHTML or Documentimo (which will be migrated back to PSWriteWord at some point). It uses DSL (Domain-Specific Language) to help build your document in an easy-to-use way. I've created few basic functions, but surely in future I will try to add more and more of those to make sure it's possible to create feature-rich PDF files.
New-PDF {
New-PDFText -Text 'Hello ', 'World' -Font HELVETICA, TIMES_ITALIC -FontColor GRAY, BLUE -FontBold $true, $false, $true
New-PDFText -Text 'Testing adding text. ', 'Keep in mind that this works like array.' -Font HELVETICA -FontColor RED
New-PDFText -Text 'This text is going by defaults.', ' This will continue...', ' and we can continue working like that.'
New-PDFList -Indent 3 {
New-PDFListItem -Text 'Test'
New-PDFListItem -Text '2nd'
}
New-PDFText -Text 'Hello ', 'World' -Font HELVETICA, TIMES_ITALIC -FontColor GRAY, BLUE -FontBold $true, $false, $true
New-PDFText -Text 'Testing adding text. ', 'Keep in mind that this works like array.' -Font HELVETICA -FontColor RED
New-PDFText -Text 'This text is going by defaults.', ' This will continue...', ' and we can continue working like that.'
New-PDFList -Indent 3 {
New-PDFListItem -Text 'Test'
New-PDFListItem -Text '2nd'
}
} -FilePath "$PSScriptRoot\Example01_Simple.pdf" -Show
What we did above is we created a PDF document, added few texts to it using New-PDFText functions and created a list with 2 bullet points. What's important here is iText 7 brings some constant values for colors, fonts, and other types of styling. Most likely it's possible to expand beyond what is built-in using a different approach, but I didn't have time to play around those options. This means it's very basic in what it can do.
New-PDF -MarginTop 100 {
New-PDFPage -PageSize A5 {
New-PDFText -Text 'Hello ', 'World' -Font HELVETICA, TIMES_ITALIC -FontColor GRAY, BLUE -FontBold $true, $false, $true
New-PDFText -Text 'Testing adding text. ', 'Keep in mind that this works like array.' -Font HELVETICA -FontColor RED
New-PDFText -Text 'This text is going by defaults.', ' This will continue...', ' and we can continue working like that.'
New-PDFList -Indent 3 {
New-PDFListItem -Text 'Test'
New-PDFListItem -Text '2nd'
}
}
New-PDFPage -PageSize A4 -Rotate {
New-PDFText -Text 'Hello 1', 'World' -Font HELVETICA, TIMES_ITALIC -FontColor GRAY, BLUE -FontBold $true, $false, $true
New-PDFText -Text 'Testing adding text. ', 'Keep in mind that this works like array.' -Font HELVETICA -FontColor RED
New-PDFText -Text 'This text is going by defaults.', ' This will continue...', ' and we can continue working like that.'
New-PDFList -Indent 3 {
New-PDFListItem -Text 'Test'
New-PDFListItem -Text '2nd'
}
}
} -FilePath "$PSScriptRoot\Example01_WithSections.pdf" -Show
As you can see above, the output from the code gave us two pages with different page sizes and rotations. It's important to understand that while the name of a function is New-PDFPage, it's not exactly a page. It's more of an area or a section. If you had enough text on the first “page”, it would span across multiple pages. New-PDFPage would create a new area starting from another page. Maybe it should be called New-PDFArea but seemed less intuitive. There's also a New-PDFOptions function that allows you to define margins for the whole document, but it isn't necessary. Both New-PDF and New-PDFPage have their margin parameters, making it a bit more direct approach where the margins get applied. As we have seen above, when we used margins for New-PDF, it applied to all pages. However, it's possible to apply margins using New-PDFPage, which can have different margins per each “page”. If you want to control margins for all pages, using them on New-PDF is the best choice.
New-PDF -MarginLeft 120 -MarginRight 20 -MarginTop 20 -MarginBottom 20 -PageSize B4 -Rotate {
New-PDFText -Text 'Test ', 'Me', 'Oooh' -FontColor BLUE, YELLOW, RED
New-PDFList {
New-PDFListItem -Text 'Test'
New-PDFListItem -Text '2nd'
}
} -FilePath "$PSScriptRoot\Example01_MoreOptions.pdf" -Show
Below is another example that shows using Margins on different levels and how they apply.
New-PDF -MarginTop 200 {
New-PDFPage -PageSize A5 {
New-PDFText -Text 'Hello ', 'World' -Font HELVETICA, TIMES_ITALIC -FontColor GRAY, BLUE -FontBold $true, $false, $true
New-PDFText -Text 'Testing adding text. ', 'Keep in mind that this works like array.' -Font HELVETICA -FontColor RED
New-PDFText -Text 'This text is going by defaults.', ' This will continue...', ' and we can continue working like that.'
New-PDFList -Indent 3 {
New-PDFListItem -Text 'Test'
New-PDFListItem -Text '2nd'
}
}
New-PDFPage -PageSize A4 -Rotate -MarginLeft 10 -MarginTop 50 {
New-PDFText -Text 'Hello 1', 'World' -Font HELVETICA, TIMES_ITALIC -FontColor GRAY, BLUE -FontBold $true, $false, $true
New-PDFText -Text 'Testing adding text. ', 'Keep in mind that this works like array.' -Font HELVETICA -FontColor RED
New-PDFText -Text 'This text is going by defaults.', ' This will continue...', ' and we can continue working like that.'
New-PDFList -Indent 3 {
New-PDFListItem -Text 'Test'
New-PDFListItem -Text '2nd'
}
}
} -FilePath "$PSScriptRoot\Example01_WithSectionsMargins.pdf" -Show
$Document = Get-PDF -FilePath "$PSScriptRoot\Example01_WithSections.pdf"
$Details = Get-PDFDetails -Document $Document
$Details | Format-List
$Details.Pages | Format-Table
Close-PDF -Document $Document
You can also notice that I've used additional code below to read the PDF I've just created and read the details of that PDF file.
Here's how the output of Get-PDFDetails look like
$Document = Get-PDF -FilePath "$PSScriptRoot\Example01_WithSections.pdf"
$Details = Get-PDFDetails -Document $Document
$Details | Format-List
$Details.Pages | Format-Table
Close-PDF -Document $Document
Notice how there are additional details for pages. You probably also noticed that margins show a bit different story. This is a known issue as I am not sure how to get margins for each page separatly. Hopefully, sooner or later, I'll figure it out, and this gets updated.