Thursday, July 16, 2015

Open Source PDF Generation - Working example

wkhtmltopdf - An open source command line tool to convert HTML(s) to PDF,
  1. Download the wkhtmltopdf from the website mentioned above. You should have these files downloaded from the website: wkhtmltopdf.exe, libeay32.dll, libgcc_s_dw2-1.dll, ssleay32.dll
  2. There is an extensive help available on the website. But in case if you want to explore options start with wkhtmltopdf.exe --extended-help
  3. The example below assumes generating pdf with 
    • 3 html files (Summary page - "SampleHTML.htm", Detailed page - "Totals.htm", Footer for every page - "Footer.htm")
    • 2 images (You can keep any png image and name it as logo1.png and logo2.png. These images will be used in Summary page, let's say, as company logo)
    • At the end of this step you should have 9 files: wkhtmltopdf.exe, libeay32.dll, libgcc_s_dw2-1.dll, ssleay32.dll, 2 images (logo1.png, logo2.png) and 3 html files ("SampleHTML.htm", "Totals.htm", "Footer.htm") in the same folder. Sample HTML and images can be downloaded here
  4. Now play around with the exe to generate the pdfs. Here are some sample commands. Run this in the command prompt. Change the directory in the command prompt to the directory where 9 files are stored and execute the below commands for pdf generation.
  • wkhtmltopdf.exe --margin-bottom 15mm --margin-left 20mm --margin-right 20mm  --footer-center "Page [page]" --footer-font-size 7  SampleHTML.htm Totals.htm SampleWithFooterPageNumbers.pdf
  • wkhtmltopdf.exe --margin-bottom 15mm --margin-left 20mm --margin-right 20mm  --footer-html footer.htm SampleHTML.htm Totals.htm SampleWithFooterAddress.pdf
  • wkhtmltopdf.exe --margin-bottom 15mm --margin-left 20mm --margin-right 20mm  --footer-html footer.htm SampleHTML.htm Totals.htm SampleWithFooterAddressAndPageNo.pdf

No comments: