Forum How do I...?

Match PDF dimensions to HTML content

justin.barkhuff
I need the width/height of my PDF to correspond to the width/height of my HTML. For example, if my rendered HTML ends up being 600px x 400px, I'd like my PDF to be the same size (or at least the same aspect ratio).

Unfortunately, I don't know what the width/height of the HTML will be before it is rendered. If I knew the width/height beforehand, I could use the @page CSS property.

Is there a way to tell the PDF to match the size of the HTML content? Or is there a way to use JavaScript to calculate the page size onload and set the @page property accordingly?

Edited by justin.barkhuff

mikeday
No, this is not possible with Prince at the moment. In the future it may be possible to do this in two passes, by rendering the content, checking the size with JavaScript, setting the page size and then re-rendering. But we don't have an API for this yet. Is there any particular reason you need variable size pages?
justin.barkhuff
Ultimately, we're creating JPG previews of HTML content. We don't know exactly how tall or wide the HTML content will be before hand. The JPG needs to match the size of the HTML content.

We haven't found any good HTML-to-JPG tools, so we're going HTML-to-PDF-to-JPG. HTML-to-PDF is done with princexml, and PDF-to-JPG is done via ghost script.

Any ideas for a better HTML-to-JPG solution?
justin.barkhuff
Follow up to my previous comment: the HTML-to-JPG solution would need to be *nix compatible, and be able to run programatically. JPGs need to be generated automatically, without user intervention.
mikeday
PhantomJS perhaps?
justin.barkhuff
Thanks. PhantomJS looks promising.