Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How can I generate a pdf from html/css?

by slackert (Novice)
on Feb 16, 2009 at 19:17 UTC ( #744177=perlquestion: print w/ replies, xml ) Need Help??
slackert has asked for the wisdom of the Perl Monks concerning the following question:

I have a reporting script that generates an html document with inline css, a formatted text document, and a csv text version as well. It also sends a email with an inline html/css document as well as attaching the two text files. The html/css get rendered differently depending on the OS and email client combination and configuration. Creating a pdf should help to provide a consistent view among clients. I would like to create a pdf from the html I generate, but I want to somehow apply the css to the html before generating the pdf. This doesn't seem like a very complex thing to do, but my simple brain can't figure it out. Any ideas? Thanks!

Comment on How can I generate a pdf from html/css?
Re: How can I generate a pdf from html with css?
by ww (Bishop) on Feb 16, 2009 at 19:34 UTC
    Re your title, you can't use CSS to generate a .pdf from html.

        ;P-)

    Re your actual question, check out the possibilities at CPAN or (if you're limited to windows) ppm search pdf.

    Update re the first paragraph: OP's original title was "How can I generate a pdf from html with css?"

Re: How can I generate a pdf from html with css?
by Anonymous Monk on Feb 16, 2009 at 19:36 UTC
    This doesn't seem like a very complex thing to do, but my simple brain can't figure it out.
    Because it is a very complex thing to do. What you do is install a YOURFORMAT2pdf program, and then you use it :)
Re: How can I generate a pdf from html with css?
by samtregar (Abbot) on Feb 16, 2009 at 19:37 UTC
    Not possible, at least not with the tools I've used to create PDFs. I've used htmldoc but that doesn't have much support for CSS. I've also seen PDF::Template in action, but that uses an abstract XML format, not HTML+CSS.

    I think you'll need to approach building a PDF as a new format, not as a way to layout your existing HTML format.

    -sam

Re: How can I generate a pdf from html with css?
by dorward (Curate) on Feb 16, 2009 at 19:44 UTC

    I've heard good reports about Prince XML. It isn't Perl, but can be called from it. The major disadvantage is the price.

      Does it cost a princely sum?

      UPDATE: Holy cow, it sure does. $495 single user, $3,800 for a server!

      -sam

Re: How can I generate a pdf from html/css?
by wazoox (Prior) on Feb 16, 2009 at 20:28 UTC
    You probably should start from some other common documentation format such as perldoc, Robodoc, Javadoc, DocBook.... from which you can easily generate at will HTML, PDF, RTF or whatever format you wish.
Re: How can I generate a pdf from html with css?
by CountZero (Bishop) on Feb 16, 2009 at 21:27 UTC
    I went that way but abandoned it. HTML is for a computer screen and --if well written-- will adapt to a variety of screenwidths, available fonts, font-sizes, OS-styles, browser peculiarities, ...

    PDF on the other hand is really for printing things on paper and trying to do so exactly the same wherever it is printed.

    HTML does not even have a concept of page-length, so how would you reliably split your HTML-"pages" into PDF-pages.

    As I have to produce PDF-documents I now do so through Template Toolkit which writes me a LaTeX-file which I then feed to the pdfLaTeX program and it produces a nice PDF file.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      > HTML does not even have a concept of page-length, so how would you reliably split your HTML-"pages" into PDF-pages.

      Well CSS2 has separate "@media" styles which you can be used for printing. This has a concept of page-length!!!

      But AFAIK that's only supported by browsers and you have to fiddle around with preset browsersettings like pagemargins and pagenumber and so on... not really a reliable approach for server programming.

      Anyway I'm using this for printing¹ some private html-pages with firefox...

      Cheers Rolf

      UPDATE: Link (German): http://de.selfhtml.org/css/eigenschaften/printlayouts.htm

      (¹) ...and of course I can also directly print to a PDF-file.

Re: How can I generate a pdf from html/css?
by boblawblah (Scribe) on Feb 17, 2009 at 01:25 UTC
    This is something I have been looking for as well. Unfortunately, there is no such utility (that I have found) to convert HTML to PDF with CSS support. Here are the utilities that I have come across that you may be interested in:

    HTMLDoc - Provides a free version (which you must compile form source or find a binary on the internet, which the provide links to) and a commercial version. Provides basic HTML support (no CSS), with some limitations (for instance - try using tables within tables to layout your pages). There is a corresponding perl module to interact with HTMLDoc.

    html2ps - Converts html to postscript - not sure if supports conversion to pdf as well.

    A possible alternative is to use the Gtk2::MozEmbed to render the html and print to then print to PDF. I was not able to get Gtk2::MozEmbed to work on windows (if anybody has, please contact me).
Re: How can I generate a pdf from html/css?
by scorpio17 (Monsignor) on Feb 17, 2009 at 14:24 UTC

    Think of the output format as the "view" part of the MVC pattern (where the page content is your data). You need one view to generate HTML (for the web page version) and another view to generate PDF (for high-quality hard-copies).

    If you already have the data, and the data->html view, all you need is the data->pdf view, which is most likely much easier to write than html->pdf.

Re: How can I generate a pdf from html/css?
by Anonymous Monk on Feb 25, 2009 at 04:32 UTC
    Thanks every one for your help. Building the PDF myself seems to be what I need to do.
      see this http://artisan.karma-lab.net/node/1711 particularly url-renderer.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://744177]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (16)
As of 2014-09-30 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (364 votes), past polls