Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

PDF::Tk Background Image

by qwertywolf (Initiate)
on Oct 24, 2016 at 22:00 UTC ( [id://1174629]=perlquestion: print w/replies, xml ) Need Help??

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

I am new to PERL and don't even know where to start. This code was written before me and they want me to use PDF::Tk now.

What it does is takes the HTML that is in the CGI, it puts those into lines, turns those lines into PS lines and then turns those PS lines into PDF lines and prints out the PDF.

So after the PDF lines are printed we need to stick an image behind all the lines of text. Can anyone help with that?

sub printFilePdf { my $unique_id = shift; my ($file) = "$OUTFILES/$unique_id.html"; open(my $htmlFH, '<', $file) or die "Can't open file $file $!\n"; my $processId = open2(\*POUT, \*PIN, qq(html2ps -U -f /home/apache +/cgi-bin/test/html2psrc-tst)); my @lines = <$htmlFH>; print PIN @lines; close PIN; my @psLines; while (<POUT>) { chomp; push(@psLines,$_); } waitpid $processId, 0; $processId = open2(\*POUT, \*PIN, qq(ps2pdf -sPAPERSIZE=letter - - +)); print PIN "$_\n" foreach(@psLines); close PIN; my @pdfLines; while (<POUT>) { chomp; push(@pdfLines, $_); } waitpid $processId, 0; #I am thinking right about here print "Content-Type: application/pdf\n"; print "Content-Disposition: attachment; filename=record.pdf\n\n"; print "$_\n" foreach(@pdfLines); }

Replies are listed 'Best First'.
Re: PDF::Tk Background Image
by soonix (Canon) on Oct 25, 2016 at 10:38 UTC
    From looking at the source of PDF::Tk, I think you should be able to use it like this (completely untested!)
    use PDF::Tk; my $pdftk=PDF::Tk->new(pdftk=>'/usr/bin/pdftk'); ... # instead of filling @pdfLines from <POUT> my $pdf_with_bg = ""; # to be filled by pdftk my $pdf_in_memory; { local $/ = undef; $pdf_in_memory = <POUT>; } # whole file as one big + string $pdftk->call_pdftk(\$pdf_in_memory, \$pdf_with_bg, 'background', '/pat +h/to/background.pdf'); print "Content-Type: application/pdf\n"; print "Content-Disposition: attachment; filename=record.pdf\n\n"; print $pdf_with_bg;
      EEEEEEEEE!!!! THIS WORKS!!! OMG I AM SO HAPPY!!! Okay, there is just one issue and I hope it's a small one. My generated text, has a white background that I can't seem to make transparent because the PDF ignores all of my CSS. Any ideas?
        Huh? Transparent background? What would you want to see through that?
        Or is it the "foreground" that is non-transparent? Then perhaps your first step (html2ps or ps2pdf) explicitly sets the background color to "white". Try to omit that.

        Update: from what I could google about html2ps, it defaults to a white background, so you'd have to modify your html2psrc-tst file.
        If you're doing this anyway, perhaps you can integrate the new background there, so that you eliminate the need for PDF::Tk …

        I don't think PDF supports transparent backgrounds... Perhaps you want to make a .png file?
Re: PDF::Tk Background Image
by Discipulus (Canon) on Oct 25, 2016 at 10:28 UTC
    Hello qwertywolf and welcome to the monastery!

    PDF manipulation is not my field, if I even have a field, but maybe there are simpler way to put an image as background of a PDF; see PDF with Background Image VIA Perl for various options.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-24 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found