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


by josera (Beadle)
on Aug 02, 2005 at 13:57 UTC ( #480185=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I'm new and this is my first post in perlmonks, i hope make it good :D. I've made a program that generates some HTML pages with SVG graphics incrusted on it. I want to convert the HTML pages to a PDF solid file to make it easier to distribute, but i haven't found a perl module in cpan that could make what i want. I'm disposed to rewrite the code of the functions to generate a PDF-object instead of the SVG files but i don't know how can i do that. The HTML pages are not very complicated, only a few tables with data, and one HTML index page. I've think that i could mix all the HTML pages into a only one bigger HTML page and convert these one. I can't use any external application, (html2ps or something like that) because the pdf has to be the output of my program. Thank you

Replies are listed 'Best First'.
Re: HTML and SVG to PDF
by holli (Abbot) on Aug 02, 2005 at 14:12 UTC
    I hope you used templating to produce your html? If so, you could replace your html-templates and write xsl-fo instead, which you then can feed to fop and produce pdf. fop is capable of handling the svg sources and turning it into an image.

    I've just read the OP wants not to use an external program, but alas, I've written this and so I post it.

    In that case, you can try if PDF::Template leads you somewhere, but i doubt it has SVG support, so you would have to fall back to an external library anyway, maybe Image::Magick.

    holli, /regexed monk/
Re: HTML and SVG to PDF
by jhourcle (Prior) on Aug 02, 2005 at 14:43 UTC
    I can't use any external application, (html2ps or something like that) because the pdf has to be the output of my program.

    Are temporary files permitted? If so, you can use an external app, then read the generated file back in and send it to STDOUT.

    Also, a quick glance at PDF::FromHTML suggests that it uses PDF::Template for its generation of the PDF, so you should be able to modify the example to:

    my $pdf = PDF::FromHTML->new( encoding => 'utf-8' ); $pdf->load_file('source.html'); $pdf->convert( Font => '/path/to/font.ttf', LineHeight => 10, Landscape => 1, ); print "Content/type: application/pdf\n\n", $pdf->pdf->get_buffer;
      Hi: I think that i'm going to generate a tex file and convert the SVG files to PNG using imagemagick. After that, i call pdflatex and i obtain the pdf file. I can't install programs, but miktex only has to be uncompressed in a folder. Thank you.
Re: HTML and SVG to PDF
by Tanktalus (Canon) on Aug 02, 2005 at 14:37 UTC

    I'm aware you say you "can't use any external application". However, you may actually be able to do so. (Disclaimer: you don't really give a reason why this won't work, so I'm going to assume it can.)

    First off, a brief point about the mentality of unix, where perl derives much of its design from. That is, do one thing and do it well. There are programs out there that do pdf conversions, do that well, and that's all they do. There is significant advantage to using them: they work, they're efficient, and you don't need to debug them.

    Next. No one says that you need to create an HTML file and tell the user to call html2ps. Or to create an XML file and tell the user to call fop. You can create the HTML or XML files, and call html2ps or fop yourself. Automatically. With no user intervention. Yes, you'll have an external dependancy. But the output of your program is still a simple PDF. You can concentrate, then, on aspects of your application that are directed towards content without worrying about how to embed SVG into PDF or anything silly like that. Let the converters deal with those details. Do your job, do it well, and leave parts that aren't relevant to your problem to others.

    You'll save a lot of time that way. ;-) You may incorporate those other programs into your solution, but they aren't really part of the problem you're solving, which is to get information to the proper users. Concentrate on that. :-)

      Hello, Thank you for your idea, i think the same that you, if something is done and it works good, why should you do again? Securely you'll do something worse. The problem is that my computer only has installed Windows, i can't run Linux, and i have no permission to install programs, i think that html2ps it's a good alternative, but i have to install the program imagemagick to work it together and i can't because i haven't got enough privileges, so i don't know how can i do that, because the module that i've found to write a PDF are low level, and they are very complicated to use. I hope to find something... Thank you anyway
        The problem is that my computer only has installed Windows, i can't run Linux, and i have no permission to install programs
        Go, ask the admin ;-)

        Seriously, the tools mentioned here don't have to be installed. fop is a java program that can simply be copied to somewhere in the file system you have access to. I don't know for sure, but i think you can simply copy and use the other tools too. (except for imagemagick which comes with a installer iirc.)

        sidenote: is this a task at your workplace? if so, you really should talk to your boss and explain what you need, so he/she can tell the sysadmins to install that.

        holli, /regexed monk/
        and i have no permission to install programs, ...

        Here's a memorable line from Zaxo, in reply to a poster with a similar problem

        . . . without using any module.
        Who is handicapping you in this way? Do they have any stake in the result?


Re: HTML and SVG to PDF
by JediWizard (Deacon) on Aug 02, 2005 at 14:06 UTC

    I don't know much about SVG graphics... but I have used HTML::HTMLDoc to create PDF documents from HTML with pleasant results.

    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol

      Not suitable to the OP's specification. You need to install the htmldoc program.

      holli, /regexed monk/
      Hello, Thank you for your idea, but i've read that for use HTML::HTMLDoc, i have to install the program htmldoc, haven't i? The problem is that i can't install HTMLDoc...
Re: HTML and SVG to PDF
by aquarium (Curate) on Aug 02, 2005 at 14:21 UTC
    depending on if it's a once of or can setup a pdf (virtual) printer to deliver any page on your site as pdf OR for a single doc just load the page in and save as pdf. there are pdf modules for perl, of varying quality but, that may involve more coding than is necessary to get the job done. google for "pdf perl"
    the hardest line to type correctly is: stty erase ^H
      Hello, Thank you, but the output of the program has to be a PDF file, i can't use OpenOffice or something like that.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2022-11-29 00:56 GMT
Find Nodes?
    Voting Booth?