Editing/Replacing Text in a PDF -- PDF::API2

This is a multi-part message in MIME format.

--===============0090985793==
Content-Class: urn:content-classes:message
Content-Type: multipart/alternative;
boundary="----_=_NextPart_001_01C72449.296C6772"

This is a multi-part message in MIME format.

------_=_NextPart_001_01C72449.296C6772
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

ok I will try to be as detailed as possible, I have to create a PDF from
variables posted from Flash. getting the variables is easy, and putting
them in a PDF however was challenging when first started is now for the
most part under control. the tricky part it, the layout for the pdf is
designed using Adobe Indesign, I use perl to open the pdf and to place
variables in the template (PDF::API2), but the designer makes the text
in Indesign and wants me to replace his text holders with the variables
posted from flash. Right now I have been trying to just replace text,
and nothing works this is the code i have thus far.

#!/usr/bin/perl

use PDF::API2;
use GD::Graph::bars;
use GD::Graph;
use GD;
use strict;
use constant mm =3D> 25.4/72;
use constant in =3D> 1/72;
use constant pt =3D> 1;

$pdf =3D PDF::API2->open('InDesignTemp.pdf');

my [at] data =3D (
["Jan-01","Feb-01","Mar-01"],
[21,25,33]
);

my $graph =3D new GD::Graph::bars;

$graph->set(
x_label =3D> 'Month',
y_label =3D> 'Revenue ($1,000s)',
title =3D> 'Monthly Online Sales for 2001',
bar_spacing =3D> 10
)
or warn $graph->error;

$graph->plot(\ [at] data) or die $graph->error;

my %font =3D (
Helvetica =3D> {
Bold =3D> $pdf->corefont('Helvetica-Bold', -encoding =3D> =
'lat
+in1'),
Roman =3D> $pdf->corefont('Helvetica', -encoding =3D> =
'lati
+n1'),
Italic =3D> $pdf->corefont('Helvetica-Oblique', -encoding =3D> =
'lat
+in1'),
},
Times =3D> {
Bold =3D> $pdf->corefont('Times-Bold', -encoding =3D> =
'lati
+n1'),
Roman =3D> $pdf->corefont('Times', -encoding =3D> =
'la
+tin1'),
Italic =3D> $pdf->corefont('Times-Italic', -encoding =3D> =
'lat
+in1'),
},
);

my $page =3D $pdf->openpage(1);

s/Title/Ben is the Man/;

my $headline_text =3D $page->text;
$headline_text->font( $font{'Helvetica'}{'Bold'}, 18/pt );
$headline_text->fillcolor( 'white' );
$headline_text->translate( 50/mm, 130/mm );
$headline_text->text_right( 'this is my new stuff' );

my $photo =3D $page->gfx;

open(GRAPH,">images/graph1.jpeg") || die "Cannot open graph1.jpg: $!\n
+";
binmode GRAPH;
print GRAPH $graph->gd->jpeg(100);
close GRAPH;

$photo_file=3D$pdf->image_jpeg("images/graph1.jpeg");
$photo->image($photo_file, 60/mm, 70/mm, 85/mm, 75/mm);

$pdf->saveas("Build3.pdf");

############################################################ ##########
+###################
####this part I was using to to replace the text however it corrupts t
+he file after i do
############################################################ ##########
+####################
#open(PDF,"<Build3.pdf") || print header('text/html').'can\'t open PDF
+ file';
#binmode PDF;
#undef $/;
#$_ =3D <PDF>;
#close(PDF);

#s/Title/Ben is the Man/;


#open(OUT, '>out.pdf') || print header('text/html').'can\'t open out f
+ile';
#print OUT $_;
#close(OUT);





------_=_NextPart_001_01C72449.296C6772
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<html>

<head>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Dus-ascii">


<meta name=3DGenerator content=3D"Microsoft Word 10 (filtered)">

<style>
<!--
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman";}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;}
p
{margin-right:0in;
margin-left:0in;
font-size:12.0pt;
font-family:"Times New Roman";}
pre
{margin:0in;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:"Courier New";}
tt
{font-family:"Courier New";}
span.EmailStyle17
{font-family:Arial;
color:windowtext;}
[at] page Section1
{size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
{page:Section1;}
-->
</style>

</head>

<body lang=3DEN-US link=3Dblue vlink=3Dpurple>

<div class=3DSection1>

<p><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:12.0pt'>ok I will
try to be as detailed as possible, I have to create a PDF from variables =
posted
from Flash. getting the variables is easy, and putting them in a PDF =
however
was challenging when first started is now for the most part under =
control. the
tricky part it, the layout for the pdf is designed using Adobe Indesign, =
I use perl
to open the pdf and to place variables in the template (PDF::API2), but =
the
designer makes the text in Indesign and wants me to replace his text =
holders
with the variables posted from flash. Right now I have been trying to =
just
replace text, and nothing works this is the code i have thus far. =
</span></font></p>

<pre><tt><font size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>#!/usr/bin/perl</span></font></tt></pre><pre><=
tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>use =
PDF::API2;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>use =
GD::Graph::bars;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>use =
GD::Graph;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>use =
GD;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>use =
strict;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>use =
constant mm =3D> 25.4/72;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>use =
constant in =3D>    =
1/72;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>use =
constant pt =3D>    =
1;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>$pdf =3D =
PDF::API2->open('InDesignTemp.pdf');</span></font></tt></pre><pre><tt>=
<font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>my [at] data =
=3D (</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>["Jan-01","Feb-01","M=
ar-01"],</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> =
[21,25,33]</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> =
);</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> my =
$graph =3D new GD::Graph::bars;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> =
$graph->set(</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>x_label =
=3D> 'Month',</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> y_label =
=3D> 'Revenue ($1,000s)',</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> title =
=3D> 'Monthly Online Sales for =
2001',</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> =
bar_spacing =3D> 10</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> =
)</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> or warn =
$graph->error;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> =
$graph->plot(\ [at] data) or die =
$graph->error;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>my %font =
=3D (</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>    Helvetica =3D> =
{</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>       =
Bold   =3D> =
$pdf->corefont('Helvetica-Bold',    -encoding =3D> =
'lat</span></font></tt></pre><pre><tt><font
size=3D2 color=3Dred face=3D"Courier New"><span =
style=3D'font-size:10.0pt;color:red'>+</span></font>in1'),</tt></pre><pre=
><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>      Roman  =
=3D> =
$pdf->corefont('Helvetica',       &=
nbsp; -encoding =3D> 'lati</span></font></tt></pre><pre><tt><font
size=3D2 color=3Dred face=3D"Courier New"><span =
style=3D'font-size:10.0pt;color:red'>+</span></font>n1'),</tt></pre><pre>=
<tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>       Italic =
=3D> $pdf->corefont('Helvetica-Oblique', -encoding =3D> =
'lat</span></font></tt></pre><pre><tt><font
size=3D2 color=3Dred face=3D"Courier New"><span =
style=3D'font-size:10.0pt;color:red'>+</span></font>in1'),</tt></pre><pre=
><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>    =
},</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> =
   Times =3D> {</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>      =
Bold   =3D> =
$pdf->corefont('Times-Bold',       =
-encoding =3D> 'lati</span></font></tt></pre><pre><tt><font
size=3D2 color=3Dred face=3D"Courier New"><span =
style=3D'font-size:10.0pt;color:red'>+</span></font>n1'),</tt></pre><pre>=
<tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>        =
Roman  =3D> =
$pdf->corefont('Times',        =
;     -encoding =3D> =
'la</span></font></tt></pre><pre><tt><font
size=3D2 color=3Dred face=3D"Courier New"><span =
style=3D'font-size:10.0pt;color:red'>+</span></font>tin1'),</tt></pre><pr=
e><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>       Italic =
=3D> $pdf->corefont('Times-Italic',      =
-encoding =3D> 'lat</span></font></tt></pre><pre><tt><font
size=3D2 color=3Dred face=3D"Courier New"><span =
style=3D'font-size:10.0pt;color:red'>+</span></font>in1'),</tt></pre><pre=
><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>    =
},</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> =
);</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>my $page =
=3D $pdf->openpage(1);</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>s/Title/Ben is the =
Man/;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> my =
$headline_text =3D =
$page->text;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> =
$headline_text->font( $font{'Helvetica'}{'Bold'}, 18/pt =
);</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> =
$headline_text->fillcolor( 'white' =
);</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> =
$headline_text->translate( 50/mm, 130/mm =
);</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'> =
$headline_text->text_right( 'this is my new stuff' =
);</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>my $photo =
=3D $page->gfx;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>open(GRAPH,">images/graph1.jpeg")=
|| die "Cannot open graph1.jpg: =
$!\n</span></font></tt></pre><pre><tt><font
size=3D2 color=3Dred face=3D"Courier New"><span =
style=3D'font-size:10.0pt;color:red'>+</span></font>";</tt></pre><pr=
e><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>binmode =
GRAPH;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>print =
GRAPH =
$graph->gd->jpeg(100);</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>close =
GRAPH;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>$photo_file=3D$pdf->image_jpeg("images=
/graph1.jpeg");</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>$photo->image($photo_file, 60/mm, 70/mm, =
85/mm, 75/mm);</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>$pdf->saveas("Build3.pdf");</span=
></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>##############################################=
########################</span></font></tt></pre><pre><tt><font
size=3D2 color=3Dred face=3D"Courier New"><span =
style=3D'font-size:10.0pt;color:red'>+</span></font>###################</=
tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>####this =
part I was using to to replace the text however it corrupts =
t</span></font></tt></pre><pre><tt><font
size=3D2 color=3Dred face=3D"Courier New"><span =
style=3D'font-size:10.0pt;color:red'>+</span></font>he file after i =
do</tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>##############################################=
########################</span></font></tt></pre><pre><tt><font
size=3D2 color=3Dred face=3D"Courier New"><span =
style=3D'font-size:10.0pt;color:red'>+</span></font>####################<=
/tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>#open(PDF,"<Build3.pdf") || =
print header('text/html').'can\'t open =
PDF</span></font></tt></pre><pre><tt><font
size=3D2 color=3Dred face=3D"Courier New"><span =
style=3D'font-size:10.0pt;color:red'>+</span></font> =
file';</tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>#binmode =
PDF;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>#undef =
$/;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>#$_ =3D =
<PDF>;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>#close(PDF);</span></font></tt></pre><pre><tt>=
<font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>#s/Title/Ben is the =
Man/;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>#open(OUT, '>out.pdf') || print =
header('text/html').'can\'t open out =
f</span></font></tt></pre><pre><tt><font
size=3D2 color=3Dred face=3D"Courier New"><span =
style=3D'font-size:10.0pt;color:red'>+</span></font>ile';</tt></pre><pre>=
<tt><font
size=3D2 face=3D"Courier New"><span style=3D'font-size:10.0pt'>#print =
OUT $_;</span></font></tt></pre><pre><tt><font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'>#close(OUT);</span></font></tt></pre><pre><tt>=
<font
size=3D2 face=3D"Courier New"><span =
style=3D'font-size:10.0pt'> </span></font></tt></pre>

<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'> </span></font></p>

</div>

</body>

</html>

------_=_NextPart_001_01C72449.296C6772--

--===============0090985793==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0090985793==--
Ben Eagle [ Mi, 20 Dezember 2006 16:11 ] [ ID #1574094 ]
Perl » gmane.comp.lang.perl.active-perl » Editing/Replacing Text in a PDF -- PDF::API2

Vorheriges Thema: Module Install problem
Nächstes Thema: DBI Oracle Windows 2003