Export To PDF

Export to PDF is used to generate a PDF file.

In SheetKraft, there is no direct function to create a PDF. The indirect process is to create an HTML file and then convert that into a PDF file.

HTML file is created with the help of an HTML template using ExportToHtml() function. The HTML file is then converted into a PDF file using the wkhtmltopdf tool.

Step 1: Convert HTML template to HTML File

This can be done using the ExportToHTML function.

Step 2: Convert the HTML File to PDF File.

HTML file can be converted to a PDF file using the wkhtmltopdf tool.

Following are the steps to convert HTML file to PDF file:

  1. Run command prompt with administrator rights.

  2. Change the directory to the folder having wkhtmltopdf.exe file.

  3. Run the following command:

Syntax:

<Pathtowkhtmltopdf.exe file> --page-size <Letter|A4> -- header-html <path to html header file>

--header-spacing <header spacing value> --footer-html <path to html footer file> --margin-bottom <bottom spacing value> <path to input HTML file> <path to output PDF file>

In the above syntax, the necessary part is in the bold font. The rest of the syntax depends on the output that you need i.e. change in page size, header or footer requirement.

For further enhancement using different arguments, you can check this page https://wkhtmltopdf.org/usage/wkhtmltopdf.txt

Input Data

For this demo, we are taking a tabular input.

Generate HTML File

We will use TabletoHTML.sk() to convert this tabular data into an HTML code and then save that in a temporary file using ExporttoHTML.sk().

Convert to PDF

We will first create the syntax using CommandLine.sk() and then execute that using RunCommand.sk().

Let us go through the syntax for a clear understanding:

"C:\Users\QP-420\Desktop\export to pdf\wkhtmltopdf.exe" Path Destination of the wkhtmltopdf tool

--log-level error It is used to limit the log generation only for errors. In SheetKraft’s RunCommand, for some command lines, the output generated is logged as an error even though it’s an output.

--header-html Syntax to add header to the output file

"C:\Users\QP-420\AppData\Local\SheetKraft\Apps\demo\Export to pdf\Inputs\header.html" Destination path of the Header HTML file

--header-spacing 10 Syntax to add space between the header and the main data

C:\Users\QP-420\AppData\Local\SheetKraft\Temp\b73294562345423db9aff253cacb96bc\temp_html.html Generated HTML file.

"C:\Users\QP-420\AppData\Local\SheetKraft\Apps\demo\Export to pdf\Outputs\Final.pdf" Output PDF file name and path

>nul To discard any output generated via RunCommand.sk()

Output

The PDF file generated including the header file.

Note

Do not keep the wkhtmltopdf.exe file in the activity folder, keep it in a different static folder. The size is around 30 MB that will be an unnecessary add-on if kept in the activity folder.

Use CommandLine.sk to create commands.