http://www.perlmonks.org?node_id=755776

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

Can any give me any pointers or an idea of how to convert a text file to a PDF file using PDF::Create?

I've googled for the past couple days and all I can find is recommendations that PDF::Create is a module to use when doing the conversion but nothing specific as to how to do it. The documentation isn't too great either.

Can anyone assist?

Thanks
Dan
  • Comment on convert text file to pdf with PDF::Create

Replies are listed 'Best First'.
Re: convert text file to pdf with PDF::Create
by almut (Canon) on Apr 06, 2009 at 17:15 UTC

    Here's a simple example, essentially using printnl() to put the lines of your text on the page without any kind of advanced layouting:

    #!/usr/bin/perl use strict; use warnings; use PDF::Create; my $text = <<'EOT'; Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliqui +p ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. EOT my $pdf = new PDF::Create('filename' => 'mydoc.pdf', 'Title' => 'Simple Demo', ); my $root = $pdf->new_page('MediaBox' => $pdf->get_page_size('A4')); my $font = $pdf->font(); # using defaults my $page = $root->new_page(); $page->printnl($text, $font, 14, 50, 750); # text, font, size, x, y $pdf->close;

    For anything non-trivial, though, I'd personally use LaTeX (there exists a variant which directly outputs PDF), in which case you'd have to markup your text (paragraphs, headlines, etc.), and specify the style (column widths, fonts, etc.) using a well-established syntax. LaTeX would then do the layouting in a much more professional way than you could code with PDF::Create in the same time it takes to write the markup... (although there's of course a learning curve, if you're using it for the first time).  But it all depends on what exactly you want to achieve.

Re: convert text file to pdf with PDF::Create
by ramrod (Curate) on Apr 06, 2009 at 17:09 UTC
    Have you tried using the module yet?
    The example in the documentation at PDF::Create is:
    my $f1 = $pdf->font('Subtype' => 'Type1', 'Encoding' => 'WinAnsiEncoding', 'BaseFont' => 'Helvetica'); $page->string($f1, 20, 306, 396, "some text");
    I would start by reading in your text document, and putting what you read in the "some text" area.
    Post your results - I'm just as interested in how this turns out as you are.
Re: convert text file to pdf with PDF::Create
by Anonymous Monk on Apr 06, 2009 at 17:03 UTC
Re: convert text file to pdf with PDF::Create
by VinsWorldcom (Prior) on Apr 06, 2009 at 17:08 UTC

    Alternatively, try PDF995 if you're on a Windows machine. It installs as a printer so you just print your document to it and it creates the PDF. I've been using it for years with no issues.