Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

PDF::API2 Editing PDf files

by ikkon (Monk)
on Nov 13, 2006 at 20:15 UTC ( #583813=perlquestion: print w/replies, xml ) Need Help??
ikkon has asked for the wisdom of the Perl Monks concerning the following question:

I installed all the modules needed for this project... I made a script that opens a pdf file, I need to be able to find a line of text inside the pdf file and replace it with something else, or pass variable to it, I have been stumb trying to figure this out and no one on CB seems to know either help please....

2006-11-14 Retitled by planetscape, as per Monastery guidelines
Original title: 'PDF::API@ Editing PDf files'

Replies are listed 'Best First'.
Re: PDF::API2 Editing PDf files
by GrandFather (Sage) on Nov 13, 2006 at 20:21 UTC
Re: PDF::API2 Editing PDf files
by liverpole (Monsignor) on Nov 13, 2006 at 20:21 UTC
    Hi ikkon,

        I made a script that opens a pdf file

    I haven't done a single thing with PDF that I can remember.  But if you show us the script you've written, I'd be glad to try giving it a shot, and there's probably dozens of other "PDF neophytes" who would as well.

    So show us what you have.  Then someone may notice that you're not using the right module.  Or that you're calling a method with the wrong arguments.  Or that you've got a simple typographical error somewhere.  Or a myriad of other possibilities ...

    Oh ... and welcome to Perlmonks!

Re: PDF::API2 Editing PDf files
by Tanktalus (Canon) on Nov 13, 2006 at 20:20 UTC

    What have you tried (post code)? Personally, I've had very limited success in doing anything PDF-related - the perl modules have all completely garbled my PDFs when I try to work with existing PDFs that weren't originally made in perl. But perhaps someone can start with where you are to fix your code for what you need. It's just easier to do when you post your code.

    Good luck!

Re: PDF::API2 Editing PDf files
by Khen1950fx (Canon) on Nov 13, 2006 at 20:54 UTC
    If you really haven't used PDF much, then I think that you should try PDF::Reuse. There's a great tutorial for it that'll help you get used to working with PDF: PDF::Reuse::Tutorial.
Re: PDF::API Editing PDf files
by ikkon (Monk) on Nov 13, 2006 at 20:52 UTC
    I don't have to much to brag about yet all i have thus far is opening the file and saving it as something else I can get much else to work, also never worked with this module or pdf's
    #!/usr/bin/perl use PDF::API2; $pdf = DF::API2->open('Maturity Assess3.pdf'); $pdf->saveas("new.pdf"); #####to import a page and place it on another page ## $pdf = PDF::API2->new; ## $old = PDF::API2->open('my/old.pdf'); ## $page = $pdf->importPage($old,2); # get page 2 into page 1 ## $pdf->saveas("our/new.pdf");

      Nothing to do with your immediate problem, but we strongly urge you to add use strict; use warnings; to all the Perl you write. In the long run it will save us a lot of time answering your questions ;).

      DWIM is Perl's answer to Gödel
Re: PDF::API2 Editing PDf files
by zentara (Archbishop) on Nov 14, 2006 at 13:47 UTC
      well I got as far as creating a pdf and then opening and writing text, i need to write the text on the first page of the opened file but every time i try it apends it to another page.
      use PDF::API2; use DBI; use strict; use constant mm => 25.4/72; use constant in => 1/72; use constant pt => 1; $pdf = PDF::API2->open('Build.pdf'); my %font = ( Helvetica => { Bold => $pdf->corefont('Helvetica-Bold', -encoding => 'la +tin1'), # Roman => $pdf->corefont('Helvetica', -encoding => 'la +tin1'), # Italic => $pdf->corefont('Helvetica-Oblique', -encoding => 'la +tin1'), }, Times => { # Bold => $pdf->corefont('Times-Bold', -encoding => 'la +tin1'), Roman => $pdf->corefont('Times', -encoding => 'la +tin1'), # Italic => $pdf->corefont('Times-Italic', -encoding => 'la +tin1'), }, ); my $page = $pdf->page; my $headline_text = $page->text; $headline_text->font( $font{'Helvetica'}{'Bold'}, 18/pt ); $headline_text->fillcolor( 'white' ); $headline_text->translate( 19/mm, 131/mm ); $headline_text->text_right( 'this is my new stuff' ); $pdf->saveas("Build2.pdf");

        It's your my $page = $pdf->page; that's creating the new page.

        To put further content on an already existing page, simply use the method openpage instead, e.g. my $page = $pdf->openpage(1) for the first page -- see the section "PAGE METHODS" in the module's docs for the details.

Re: PDF::API2 Editing PDf files
by ikkon (Monk) on Dec 15, 2006 at 16:31 UTC
    thanks almut that worked perfectly, now would you know if there is any way to find text in the pdf and replace it s/text/replacetext/g; or something to that order, i have tried but nothing worked

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://583813]
Approved by Tanktalus
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2017-10-21 13:27 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (270 votes). Check out past polls.