New-KFPDF

Syntax

New-KFPDF 
  -HTML <string> 
  [-AsStream[:$false]] 
  [-PageWidth <string>]
  [-PageHeight <string>]
  [-MarginLeft <string>]
  [-MarginTop <string>]
  [-MarginRight <string>]
  [-MarginBottom <string>]
  [-Landscape[:$false]]
  [-BackgroundsEnabled[:$false]]
  [-PaperFormat <string>]
  [-SurroundWithPdfFrameIfHtmlTagMissing]
  [-PdfFrame <string>]
  [-HtmlCheck[:$true]]
  [-InProcess[:$false]]
  [-ThrowErrors[:$true]]

Support

>= kenaflow 1.0.0

Return

System.IO.Stream or System.Byte array.

Description

One of the great advantages of kenaflow is the integrated PDF engine, which, together with the template engine, makes it very easy to generate a PDF from simple HTML and the data from the workflow or item.

We use the open source software Chromium, encapsulated by Puppeteer.net to provide this functionality.

The example below shows how to create a PDF document from a template and upload it to SharePoint.

Parameters

-HTML <string>

The HTML string that gets converted to PDF.

[-AsStream[:$false]]

Tells the engine to return a System.IO.Stream object rather than a System.Byte array. Default (if not specified): System.Byte array.

[-PageWidth <string>] and [-PageHeight <string>]

You can provide paper height and width with these options. Please add a unit like inch or mm or cm or px.

[-MarginLeft <string>], [-MarginTop <string>], [-MarginRight <string>] and [-MarginBottom <string>]

With these settings you can adjust the margins on the page. Please use a unit like px.

[-Landscape[:$false]]

If specified the PDF pages will be generated with "landscape" orientation. If not specified the default ist "portrait".

`[-BackgroundsEnabled[:$false]]

If not specified or if explicitly set to :$false all background images or colors are not exported to PDF.

[-PaperFormat <string>]

One of the following values. A4 is default.

  • A6
  • A5
  • A4
  • A3
  • A2
  • A1
  • A0
  • Ledger
  • Legal
  • Letter
  • Tabloid

[-SurroundWithPdfFrameIfHtmlTagMissing[:$false]] and [-PdfFrame <string>]

You should always provide complete HTML, i.e. with HTML tag, HEAD tag and BODY tag. If you don't, there may be formatting problems. In case you don't have the source data under control, this parameter offers the possibility to put a standard frame around the transferred data, which should consist of an HTML, HEAD and BODY tag. The frame is defined in -PdfFrame <string>, e.g. -PdfFrame “<html><head></head><body>{{content}}</body></html>”. So if -SurroundWithPdfFrameIfHtmlTagMissing is specified and a string is passed: -HTML “<div>some test data</div>” then the placeholder {{content}} is replaced by the string in -HTML in the string used in -PdfFrame with the result: <html><head></head><body><div>some test data</div></body></html>. If a complete HTML is passed, the -PdfFrame is not applied! Attention: You need to specify :$true or :$false to actively activate or deactivate this setting and its function. If not supplied the corresponding setting from global configuration is used.

[-HtmlCheck[:$true]] If specified the string in -HTML <string> is checked with the library we use for HTML processing - HTMLAgilityPack. If there are problems with the HTML structure they are logged as warnings. This is maybe helpful during development.

[-InProcess[:$false]] In earlier versions of kenaflow we used CEFSharp and this required that we run Chromium in a separate process for stability. - This is not required any more with Puppeteer. If specified the PDF creation is done by the Workflow process itself. This is a little bit faster.

[-ThrowErrors[:$true]]

This is a default parameter for all kenaflow cmdlets. If set to $true (default!) the engine will pass exception within the cmdlet to the script for further handling.

Example

$Text = (Invoke-KFTemplate -Template "<p>Hello <b>{{Author}}</b>.</p>")
$Pdf = New-KFPDF -HTML $Text -AsStream
Add-PnPFile -Folder "/Archive" -FileName "$($item.ID).pdf" -Stream $Pdf