Real Software Forums

PDF Report generation (using wkhtmltopdf)
Page 1 of 1

Author:  Sky Stream [ Tue Aug 14, 2012 11:12 pm ]
Post subject:  PDF Report generation (using wkhtmltopdf)


given the amount of help the RealStudio community has given me over the years, I figure it was time I contribute something back that I have seen as being a need for report PDF generation for web base apps. Understand that I am not claiming that my way is the best / right way for your projects, it is just something that worked really well for my projects and figured I would share the overall concept and code that is not sensitive.

my setup:

- Currently, I am running my app as a standalone application on a Windows 2008 and Windows 2003 server on a dedicated box.
- I am running wkhtmltopdf in this environment, however note that wkhtmltopdf can also run on Mac and Linux and a few google searches show a number of people installing it on shared web hosting.

the overall concept of what I am about to present is a 4 step process:

1) Data in the database is outputted to an XML file.
2) XML file & your XSLT (reporting template) is transformed to HTML file.
3) convert HTML file to PDF using wkhtmltopdf
4) present PDF file to end user to download or open.

For steps 1 and 2, given that your milage may vary.. I will have you refer to for how to achieve these two task as they are a whole topic in itself; however, I will comment on the following:

- The premise is to output your data to an xml format in which will reference your XSLT file (which is considered your report layout / design) for how your report will need to display (I recommend visiting to review there tutorials on XSLT).

- For the purpose of this step, you want to transform the xml & xslt file into an html file.

- If you wanted to just play around with pdf conversion part, you can create a simple HTML file for the time being to test with until you decide that this report generation concept is right for your project.

For steps 3:

- During my initial search of looking for an html to pdf converter for the web, I came across an open source project called wkhtmltopdf ( in which I found to be very powerful. After looking at the syntax (, I concluded that wkhtmltopdf gave me a lot of options which allowed me to convert my html file to pdf with header, footer, page numbers, margin control, etc…

- To perform the convert operation, I am simply running wkhtmltopdf & arguments in shell mode while outputting the pdf file name to filename I have control over.

' Create PDF File

dim pName as String, htmlF, pdfF as FolderItem
dim dShell as new shell

pName = (str(Session.Identifier) + ".pdf")
pdfF = SpecialFolder.CurrentWorkingDirectory.Child("tempData").Child(pName)

dShell.Mode = 0
dShell.Execute("wkhtmltopdf.exe --margin-top 25mm --margin-bottom 20mm --header-html C:\sample_project\reportTemplates\sample_project-pt-header.html --footer-html C:\sample_project\reportTemplates\sample_project-footer.html -O Landscape " + htmlF.AbsolutePath + " " + pdfF.AbsolutePath)

Step 4:

- Once the pdf has generated behind the scenes, we want to use WebFile to present the pdf file to the end user for download / open.

// Present to User

Session.reportFile = WebFile.Open(pdfF)
Session.reportFile.mimetype = "application/pdf"
Session.reportFile.ForceDownload = true
Session.reportFile.Filename = "sample_report.pdf"


// Delete Temp Files

And that pretty much sums it up. Again, this is something I came up with that only cost me time to research & develop. I have found the key to a successful report look and feel from this way of doing report pdf generation has to do with the HTML file you are generating and what options you are choosing in wkhtmltopdf.

I hope this helps to anyone looking. :)

Author:  Akiland [ Tue Aug 14, 2012 11:40 pm ]
Post subject:  Re: PDF Report generation (using wkhtmltopdf)

Great, thank you for sharing!
It will sure come in handy some day :)

Author:  Akiland [ Tue Aug 28, 2012 10:11 am ]
Post subject:  Re: PDF Report generation (using wkhtmltopdf)

I have now integrated wkhtmltopdf into my WE app so it can create PDF reports for the user to print or save :)
I have a template HTML file that the app inserts some table data into that is converted to PDF.

Thank you for the tips above to get it working!

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group