2

The task is not easy, as I need to create a PDF version of a page after clicking a button. It has a complicated dom structure, with many elements positioned absolutely/relatively, and the data is dynamically loaded. I thought about using Phantom.js server for this task, as it enables generating PDF's but this introduces some problems ;

  • phantomjs can be either run from a console (which is not possible here as generating PDF's must be available on both UNIX and WIN systems and only shell scripts can be run from the page) or using it's built in server
  • server has a different domain as the server running the page, so same origin policy problems apear. I was thinking about using iframe with phantomjs server address as the src and my page html passed in arguments, but the arguments are too long and return 413 error. JSONP won't work, as it was built for GET requests and the same problem with too long arguments appears.

I also thought about generating it client-side but because of the complexity of the site it would probably take me many days to get proper representation of the page (as all the positioning values need to be recalculated to pts), and it has to be a generic solution.

Any other ideas how to solve this ?

mike_hornbeck
  • 1,612
  • 3
  • 30
  • 51

3 Answers3

2

I've looked at several tools to accomplish server-side PDF generation.

There is one called abcPDF (.net) from Web SuperGoo that I tried to use, but I consistently had problems with it not working.

There is another one which seemed to work much better called iText, available in both .net and Java versions. Depending on what version you use, it is available with AGPL, LGPL, and commercial licenses.

I wish I could tell you differently, but I don't know of any way to easily convert from HTML to PDF. Each of these libraries has an API that you must program against to describe what the document should look like.

Vivian River
  • 31,198
  • 62
  • 198
  • 313
1

What about using a PDF printer? Depending on your system, there could be a built-in PDF printer, or if using Windows, you can have a look at PDFCreator

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
  • cool tool but my requirement is to have the pdf generated from the browser window – mike_hornbeck May 11 '12 at 08:09
  • @mike_hornbeck PDFCreator can be invoked through the usual `File > Print...` of your browser. But maybe you meant through a button integrated **in** the webpage? – Alexis Pigeon May 11 '12 at 08:31
  • yes, sorry I wasn't precise. Forcing users to install stuff on a server is one thing, telling them to install third party applications is a completely different story :/ – mike_hornbeck May 11 '12 at 08:33
  • @mike_hornbeck Totally agree with you. Sorry for not understanding your requirements... – Alexis Pigeon May 11 '12 at 08:59
1

What about POSTing the entire DOM to the server using Ajax, rending it there as a PDF (using phantomjs), sending back a server URL (and nonce) in a JSON response? You could then create a download link or just initiate the download automatically. Would that work?

acorncom
  • 5,975
  • 1
  • 19
  • 31
  • Ive tried this previously with two different approach - phantomjs server listenting for POST requests, but this hasn't worked at all and there's no real way of debugging it. Then I've tried running it from php : http://stackoverflow.com/questions/10651320/starting-phantomjs-server-from-php-and-waiting-for-its-response but this also failed. For now I'm creating a temp html pages vie php, generate pdf's from them with phantomjs and finally merge everything with pdftk. – mike_hornbeck Jul 22 '12 at 13:43