Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Mon Jun 18, 2018 1:50 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: PDF Report generation (using wkhtmltopdf)
PostPosted: Tue Aug 14, 2012 11:12 pm 
Offline

Joined: Sun Nov 23, 2008 12:27 am
Posts: 404
All,

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 docs.realsoftware.com 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 w3schools.org 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 (http://code.google.com/p/wkhtmltopdf/) in which I found to be very powerful. After looking at the syntax (http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf_0.10.0_rc2-doc.html), 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"

Session.ShowURL(Session.reportFile.URL)

// Delete Temp Files
htmlF.Delete
pdfF.Delete



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. :)


Top
 Profile  
Reply with quote  
 Post subject: Re: PDF Report generation (using wkhtmltopdf)
PostPosted: Tue Aug 14, 2012 11:40 pm 
Offline
User avatar

Joined: Tue Jan 04, 2011 3:02 am
Posts: 1236
Location: Jönköping, Sweden
Great, thank you for sharing!
It will sure come in handy some day :)

_________________
Image http://www.linkedin.com/in/albinkiland
Dev. iMac 27" + 2x22" LG (2.8GHz Intel Core i7, 12GB RAM, 120GB SSD) OS X 10.8
Xojo Pro 2013r1


Top
 Profile  
Reply with quote  
 Post subject: Re: PDF Report generation (using wkhtmltopdf)
PostPosted: Tue Aug 28, 2012 10:11 am 
Offline
User avatar

Joined: Tue Jan 04, 2011 3:02 am
Posts: 1236
Location: Jönköping, Sweden
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!

_________________
Image http://www.linkedin.com/in/albinkiland
Dev. iMac 27" + 2x22" LG (2.8GHz Intel Core i7, 12GB RAM, 120GB SSD) OS X 10.8
Xojo Pro 2013r1


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group