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

Dear monks, I once again need some help to save my soul. I use PDF::Create module to generate pdf files. Everything is going fine except for one thing: when I print a line in the pdf file, for instance:
$pages[$page_number]->stringl($f2, 8, $x, $y, $new_string);
The tabulation character \t does not appear. So the string "This\tis\ta\tstring" will be printed as "Thisisastring". Anyone knows how to get around this? (other than replacing \t with spaces...). Cheers,

Replies are listed 'Best First'.
Re: PDF::Create - help with tabulation
by roboticus (Chancellor) on Jul 18, 2012 at 18:07 UTC

    julio_514:

    Sure ... split the string on tabs, then write each string to the column position you want. Something like:

    # Four tabstop locations: my @colpos = (8, 24, 36, 45); while (....) { my @cols = split/\t/, get_next_string(); # Print each column at its tabstop for (my $i=0; $i < @colpos; ++$i) { $pages[$page_number]->string($f2, 8, $colpos[$i], $y, $cols[$i] ); } }

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      roboticus,

      This is more a request for clarification. I have used 'PDF::API2', but I'm not familiar with 'PDF::Create'.

      Is there a command in 'PDF::Create' that allows you to use positioning without concern for the left margin and the default user space units. If there is, it seems it would be a lot easier to use than 'PDF::API2'!

      Thank you

      "Well done is better than well said." - Benjamin Franklin

        flexvault:

        I'm not familiar with PDF::Create, either. I briefly looked over the docs, and it looks like the println method might be enough. It doesn't mention tab expansion, so I don't know if the same problem would occur or not.

        There's a reference to a set width method, too, so it may let you set line length and do auto wrapping. But I haven't actually tried using it.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        UPDATE: I went ahead and installed it and gave it a quick try:

        #!/usr/bin/perl # # example for PDF::Create # use 5.14.0; use warnings; use autodie; use PDF::Create; my $txt=<<EOT; Here's a little bit of text that contains a very long line followed by + a few short ones so we can see which method(s) may or may not handle + wrapping and embedded newlines, just in case any of them do. Though\tprintnl\tshould\thandle embedded newlines just fine. EOT my $pdf = new PDF::Create('filename'=>'foo.pdf'); my $a4 = $pdf->new_page('MediaBox'=>$pdf->get_page_size('A4')); my $page = $a4->new_page; my $f1 = $pdf->font('BaseFont'=>'Helvetica'); $page->string($f1, 12, 72, 600, $txt); $page->printnl($txt, $f1, 12, 72, 400); $pdf->close;

        The result: neither the string nor printnl methods will do autowrapping or any sort of tab expansion other than treating the tabs as spaces (unless my brief scan of the docs missed an option somewhere). The printnl function will at least handle \n nicely.