Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Creating PDF files with Perl

by Anonymous Monk
on May 09, 2011 at 13:37 UTC ( [id://903763]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I am trying to creating PDF files using "HTML::HTMLDoc", that seems to be the easiest one to accomplish this using Perl. But with few issues. If you look at the code I am posting here I have a very simple HTML code that I am converting into PDF, but when I run the code the PDF is blank, the only thing that shows is the image, it displays in the new created PDF file, the rest of the code doesn't. Have anyone here worked with this before that could show me where the problem is? My other issue is that I would like to add into my PDF file a field that I can edit from the PDF, like an input type="text" in a form, is that possible? OK thanks for looking and here is the code I am testing with:

#!/usr/bin/perl -w use strict; use HTML::HTMLDoc; my $htmldoc = new HTML::HTMLDoc(); $htmldoc->set_html_content(qq| <html> <body> <br>PDF Test<br><br> <table cellpadding="0" cellspacing="0" border="1"> <tr> <td colspan="2" align="center"><img src="images/header.gif"></td> </tr> <tr> <td> <b>Phone Number: (123) 456-7897</b> </td> <td>Country Code: US</td> </tr> <tr> <td colspan=2> <b><div>Number: 00000000</div></b> </td> </tr> <tr> <td colspan="2"><b>First Name:</b> <input type="text" name="fi +rst"></td> </tr> <tr> <td colspan="2"><b>Last Name:</b> <input type="text" name="last" +></td> </tr> </table> </body> </html> |); my $pdf = $htmldoc->generate_pdf(); $pdf->to_file('test.pdf');

Thank you!

Replies are listed 'Best First'.
Re: Creating PDF files with Perl
by moritz (Cardinal) on May 09, 2011 at 13:41 UTC
    I am trying to creating PDF files using "HTML::HTMLDoc", that seems to be the easiest one to accomplish this using Perl.

    I found it easist to generate LaTeX files with Perl (for example with Text::Template), and run it through pdflatex. Your mileage might vary though.

      A similar approach is to use a templating solution, like Template-Toolkit to produce XSL-FO code, and run that trough a converter like FOP. That way one gets a better control over the layout of the resulting document.


      holli

      You can lead your users to water, but alas, you cannot drown them.
        Do you have any code sample from your previous posting?
      Would you have any sample code based in what is been posted here and based in what you are suggesting?
Re: Creating PDF files with Perl
by chrestomanci (Priest) on May 09, 2011 at 14:46 UTC

    I have had success creating pdf files by first creating an svg document for each page, then converting to pdf using inkscape's svg2pdf tool, I then joined all the separate pdf files into one multi page document using pdftk

    On that occasion the SVG file was created via a template. You could also look into GD::SVG

Re: Creating PDF files with Perl (CPAN-Review)
by LanX (Saint) on May 10, 2011 at 13:21 UTC
    Did you see Bart's negative review on CPAN?

    It includes alternative sample code achieved by RTFMing.

    Cheers Rolf

    PS: Bart++

      In my experience, htmldoc frequently dies ungracefully on HTML syntax it cannot digest, even when it is perfectly valid. So the RTFM method needs extra error handling. I do not know HTML::HTMLdoc, but the other comments indicate to me, that the module gives no good feedback on any problem.

      Now to the actual question: a field that I can edit from the PDF, like an input type="text" in a form, is that possible? No. htmldoc does only*) simple HTML 3 and simple PDF, cf www.htmldoc.org

      *)
      I still find it useful when you have HTML in the first place and can clean up a bit before passing it on to htmldoc
Re: Creating PDF files with Perl
by ww (Archbishop) on May 09, 2011 at 19:43 UTC
    And before you ask -- again -- for "sample code" from one of the responders, you might wish to RTFM.
      The reason is because all see here is people suggesting with no possible solution based on the person's code, and I see that this person posted some code sample for people to look at it, but come on, the answers here are no different than "Googling" for the topic been asked. Be reasonable, thats all!
        Due to popular demand ^^, here is a hello world example:
        use warnings; use strict; use Template; # create Template object my $ttt = Template->new(); # define template variables for replacement my $replacements = { name => "holli" }; my $template = join ("", <DATA>); # process input template, substituting variables and save it to disk $ttt->process( \$template, $replacements, "output.xml") || die $templa +te->error(); #call fop and translate xml file to pdf system "d:\\fop-1.0\\fop.bat -fo output.xml -pdf output.pdf" __DATA__ <?xml version="1.0" encoding="iso-8859-1"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="my-page"> <fo:region-body margin="1in"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="my-page"> <fo:flow flow-name="xsl-region-body"> <fo:block>Hello, [% name %]!</fo:block> </fo:flow> </fo:page-sequence> </fo:root>
        Instead of running fop via system, you might also look at XML::ApacheFOP.


        holli

        You can lead your users to water, but alas, you cannot drown them.

        I think my prior response was not unreasonable, given the appearance and reappearance of your (boilerplate) request for "sample code."

        Read the documentation for what's suggested here; read the docs that Google turns up.

        Don't ask, thinly disguised as a request for "sample code," that the Monks give you a complete solution. You'll learn a lot more from the docs and the effort to code your own solution.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://903763]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-25 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found