Gossamer Forum
Home : General : Perl Programming :

Modify a PDF?

Quote Reply
Modify a PDF?
I'm trying to work out a method to edit a PDF file. I found somecode that is 'supposed' to do this. I've edited the code a bit, so it works correctly (had a few syntax errors, such as not defining an object for CGI).

The code is as follows;

Code:
#!/usr/bin/perl

use PDF;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
$IN = new CGI;

if ($IN->param('action') eq 'make_pdf') {
open(PDF, "test.pdf") || print $IN->header('text/html')."can't open PDF file. Reason: $!";
undef $/;
$_ = <PDF>;
close(PDF);



$_ =~ s/Title/test/sig;
#s/##first_name##/$IN->param('first_name')}/;
#s/##last_name##/$IN->param('last_name')}/;



open(OUT, '>out.pdf') || print $IN->header('text/html').'can\'t open out file';
print OUT $_;
close(OUT);

print $IN->header('text/html');
print 'Your PDF has been created. It is available <a href="out.pdf">here</A>';

} else {

print $IN->header().'
<FORM ACTION="'.$ENV{SCRIPT_NAME}.'" METHOD="post">
Enter your info to make your own summersault business card in PDF format:<P>
First Name: <INPUT TYPE="text" NAME="first_name" SIZE="32" MAXLENGTH="32"><BR>
Last Name: <INPUT TYPE="text" NAME="last_name" SIZE="32" MAXLENGTH="32"><BR>
Email Address: <INPUT TYPE="text" NAME="email" SIZE="32" MAXLENGTH="32"><BR>
<INPUT TYPE="hidden" NAME="action" VALUE="make_pdf">
<INPUT TYPE="submit">
</FORM>


';
}

I don't get any errors, and the PDF seems to open find in Adobe Acrobat, but the text 'Title' is not replaced with the new string.

Does anyone have a better method to do this, or should this work? Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Modify a PDF? In reply to
*raises eyes*

http://search.cpan.org/...ery=PDF&mode=all
Quote Reply
Re: [Paul] Modify a PDF? In reply to
Yeah, I looked at that. The problem is, the example code does not work.

http://search.cpan.org/...ANTRO/PDF-111/PDF.pm

Note that $page is not defined...Also, almost the last line, they have forgotten to define the $ in front of the variables! When I fix those up, I still get an error;

Quote:
Already linked to the file test.pdf
at test.cgi line 11

Anyone got any *working* code?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Modify a PDF? In reply to
You lifted your code from here?

Two things that will help you immensely before you even start:

use strict;
use CGI::Application;

What's the code you're using with PDF? I didn't know there was a module on CPAN to edit PDFs as the PDF format is too difficult to crack?

- wil
Quote Reply
Re: [Wil] Modify a PDF? In reply to
In Reply To:
You lifted your code from here?

Yeah.

>>>What's the code you're using with PDF? I didn't know there was a module on CPAN to edit PDFs as the PDF format is too difficult to crack? <<<

I was hoping to be able to read an existing template file, replace a few things, and then write it to a PDF file. Is there a module to do this? The guy at the URL you provided seems to think that simply opening, writing etc will edit the PDF...but obviously it doesn't seem to be working Unimpressed

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Modify a PDF? In reply to
Well, in theory it might work, in practice I'm not sure it's that easy.

How does PDF encode its file format?

Have you tried opening up a PDF document with a text editor? Are there chunks of text just sitting there waiting to be regexed out like that? I very much doubt it. You need to open the PDF up in a text editor and look for the text you want to change. How is it displayed or encoded? You're probably looking for something %230fHELLO$"%!"5 or something idotic like that, not just a simple HELLO line on its own. From there you should be able to build a regex.

- wil
Quote Reply
Re: [Wil] Modify a PDF? In reply to
For example, here's a snippet of a PDF file I just created:

Code:

 {'ןs4;|{?Y{Juf^Zib;hlL[ xCE&5ef0֧/@5^><ܙە7WT`2IٙM.
):/XAUZ<%~baqUUQnM]C9ah>ɩ+6׎E5WtmHi*Z+|6:<*vV̫KM(əjHw><Ld*p;15ZC%4$>Yv&>

Think you can throw a regex to extract the sentence "HELLO" out of that one?

- wil

Last edited by:

Wil: Jun 9, 2003, 4:08 AM
Quote Reply
Re: [Wil] Modify a PDF? In reply to
As far as I know, there are no Perl modules out there that can directly modify or edit PDF files. You'll find a few that can write, or extract information, or at least meta information, from a PDF.

The reason being, I'd imagine, is that no-one knows the file format for PDF files. Same as .DOC files or any other closed source.

- wil
Quote Reply
Re: [Wil] Modify a PDF? In reply to
Yeah, I thought it was a bit too easy...but the guy who made that page seemed to think it would work. I thought it was worth a try Frown

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Modify a PDF? In reply to
Still trying to get this working. I now have the following code;

Code:
#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);
use PDF;

$pdf= PDF->new;
$pdf= PDF->new("out.pdf");

#$result=$pdf->TargetFile("testing.pdf");

print "is a pdf file\n" if ( $pdf->IsaPDF ) ;
print "Has ",$pdf->Pages," Pages \n";
print "Use a PDF Version ",$pdf->Version ," \n";
# print "and it is crypted " if ( $pdf->IscryptedPDF) ;

print "filename with title",$pdf->GetInfo("Title"),"\n";
print "and with subject ",$pdf->GetInfo("Subject"),"\n";
print "was written by ",$pdf->GetInfo("Author"),"\n";
print "in date ",$pdf->GetInfo("CreationDate"),"\n";
print "using ",$pdf->GetInfo("Creator"),"\n";
print "and converted with ",$pdf->GetInfo("Producer"),"\n";
print "The last modification occurred ",$pdf->GetInfo("ModDate"),"\n";
print "The associated keywords are ",$pdf->GetInfo("Keywords"),"\n";

my ($startx,$starty, $endx,$endy) = $pdf->PageSize ($page) ;

print "Content-type: text/html \n\n";
print "Done...";

...but it producces this when running via SSH, and a 500 IS Error when running via a browser;

Quote:
lshell-2.05a$ perl test.cgi
[Tue Jun 17 14:01:47 2003] test.cgi: Bad object reference '>' at test.cgi line 7
[Tue Jun 17 14:01:47 2003] test.cgi: Bad object reference '>' at test.cgi line 7
[Tue Jun 17 14:01:47 2003] test.cgi: Bad object reference '>' at test.cgi line 7
is a pdf file
Has Pages
Use a PDF Version 1.3
filename with title
and with subject
was written by
in date
using
and converted with
The last modification occurred
The associated keywords are
Content-type: text/html

Done...jailshell-2.05a$

Wouldn't it be nice if the demo code actually worked? Tongue (I've already had to add $'s in there, and such, the commented out line gives a function non-existant error too)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Modify a PDF? In reply to
I thought we established that what you were originally trying to do was impossible?

Have you looked in /examples/?

http://search.cpan.org/...RO/PDF-111/examples/

- wil
Quote Reply
Re: [Wil] Modify a PDF? In reply to
Looking at the example codes now. I am not trying to modify an existing PDF now, but simply create a PDF out of an HTML template, with the appropriate stuff changed before printing to the PDF.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Modify a PDF? In reply to
You looked into PDF::Create?

- wil
Quote Reply
Re: [Wil] Modify a PDF? In reply to
Getting them to install PDF::Create (havnt heard back from them yet...but i don't see why they won't install it). I have a feeling PDF.pm has a bug in it;

[Tue Jun 17 15:12:40 2003] test.cgi: Bad object reference '>' at test.cgi line 26
[Tue Jun 17 15:12:40 2003] test.cgi: Bad object reference '>' at test.cgi line 26
[Tue Jun 17 15:12:40 2003] test.cgi: Bad object reference '>' at test.cgi line 26

The code I'm using is;

Code:
#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);
use Carp;
use Getopt::Long;
use PDF;

my $version="1.0";
my $help="";
my $verbose="";

GetOptions( "help" => \$help , "verbose" => \$verbose );

$help and printusage();

foreach (@ARGV) {
do_the_dirty_job_on($_);
}

exit(1);

sub do_the_dirty_job_on {

my $file = shift;

my $PDFfile = PDF->new($file);

if ($PDFfile->IsaPDF) {
$verbose ? print "File $file has ",$PDFfile->Pages," page",$PDFfile->Pages > 1 ? "s" :"","\n"
: print $file,":",$PDFfile->Pages,"\n" ;
} else {
$verbose && print "File $file isn't a PDF file\n";
}
}

sub printusage {

print <<ANTRO;

Return the number of pages of a PDF file format.

usage:
pdf_pages [-options ...] files

where options include:
-help print out this message
-verbose verbose

or the abbreviate version -h, -v

files:
with files you can use metacharacters and relative and absolute path name

example:
pdf_pages *.pdf
pdf_pages -h
pdf_pages -v . "*.pdf" "/tmp/path/to/work/*.pdf"

ANTRO

exit(1);

};

And the command at SSh is;

perl test.cgi out.pdf

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Modify a PDF? In reply to
PDF::Create looks a lot more stable, even at version 0.1, and looks more to the job for what you want to do than PDF.

- wil