User Tools

Site Tools


Sidebar

Imprint

kb:ni-rt:linuxrt:html2pdf

31 HTML to PDF on Linux RT

To convert a HTML file to a PDF (e.g. to generate a report) there are no tools included by NI. A proper solution is the command line tool wkhtml2pdf. It is a open source tool based on the Qt WebKit-HTML engine.

Installation

Download the latest precompiled binary from https://wkhtmltopdf.org/downloads.html and copy it to the cRIO (e.g. to /home/admin). Because it's an external program we want to install it under /opt.

$ mkdir /opt
$ cd /opt
$ tar -xJf /home/admin/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
$ chown -R lvuser:ni wkhtmltox

If the tar command doesn't work (like in my case), use 7zip under Windows to extract the files and copy them to the destination folder.

Better Integration in Linux

To integrate the manually installed program better, so that you can just call it from every point in the filesystem (like other system programs), you have to create the following symbolic link.

$ cd /usr/local/bin
$ ln -s /opt/wkhtmltox/bin/wkhtmltopdf wkhtmltopdf

After a logout and login, you can execute the program by just typing wkhtmltopdf on the command line.

Usage from command line

Simple example from the command line:

$ wkhtml2pdf http://de.wikipedia.org wikipedia.pdf
$

This command downloads the Wikipedia start page and converts it to a PDF.

A little bit more complex example:

$ wkhtml2pdf -O Portrait -s A4 --dpi 300 http://de.wikipedia.org wiki-snapshot.dpf
$

With this options the PDF is printed in orientation “portrait” on a page size DIN A4 with a resolution of 300 dpi. For even more options type wkhtmltopdf -H.

Generate PDF/A for Archiving

PDF/A is an ISO-standardized version of the Portable Document Format (PDF) specialized for use in the archiving and long-term preservation of electronic documents.[https://en.wikipedia.org/wiki/PDF/A]

Unfortunately, wkHTMLtoPDF can't create PDF/A (it outputs a PDF 1.4). To get a PDF/A compliant PDF it's necessary to process the PDF generated by wkHTMLtoPDF by a second program, Ghostscript.

Ghostscript is a common open source program for processing PS- and PDF-files and is included in almost every Linux distribution (also available for Windows and Mac). Luckily it is included in the opkg repository of NI Linux RT. To generate a proper PDF/A Ghostscript needs an ICC color profile and a special configuration file (PDFA_defs.ps) which references to this profile.

Link to a sample PDFA_defs.ps file: PDFA_defs.ps The path to the ICC-profile must be inserted for PATH_TO_YOUR_ICC_FILE. Good color profiles are available for free from Adobe: Adobe ICC profiles The recommended profile for standard use cases is AdobeRGB1998.icc.

I suggest to copy the config file and the color profile to a project specific folder like PROJECT_Config/Common/.

Example installation and configuration on a cRIO:

$ opkg install ghostscript
$ ls -lah /c/HAGEMONITOR-MASTER_Config/Common
total 52
drwxrwxrwx    2 lvuser   ni          4.0K Jul  5 13:46 .
drwxrwxrwx    5 lvuser   ni          4.0K Jun 29 02:14 ..
-rw-r--r--    1 lvuser   ni           560 Jul  5 13:46 AdobeRGB1998.icc
-rw-r--r--    1 lvuser   ni          1.5K Jul  5 13:53 PDFA_def.ps
-rw-r--r--    1 lvuser   ni         33.1K May 16 03:07 logo.png

Usage of Ghostscript:

$ cd /c/HAEMONITOR-MASTER_Data
$ gs -dPDFA=2 -dBATCH -dNOPAUSE -dNOOUTERSAVE -sdUseCIEColor -sProcessColorModel=DeviceRGB -sDEVICE=pdfwrite -sOutputFile=Document_pdfa.pdf /c/HAGEMONITOR-MASTER_Config/Common/PDFA_def.ps -dPDFACompatibilityPolicy=1 Document.pdf

For more information about the command and options, see the following links:

It's possible to validate the PDF/A files with a free online tool: https://www.pdf-online.com/osa/validate.aspx

kb/ni-rt/linuxrt/html2pdf.txt · Last modified: 2019/01/09 14:24 by joerg.hampel