On May 9, 11:50=A0am, rob.di...@[EMAIL PROTECTED]
(Rob Dixon) wrote:
> hotkittywrote:
> > On May 7, 8:40 am, rob.di...@[EMAIL PROTECTED]
(Rob Dixon) wrote:
> >>hotkittywrote:
> >>> First and foremost thanks for all the help I've received on this
> >>> board, especially Gunnar who keeps this place running!
> >>> I've come a long way in my code and am trying to format some text
and
> >>> then put it into a nice pdf file. My problem is putting the
formatted
> >>> text into the pdf and for it to display correctly. I am just trying
to=
> >>> justify the text and then set the margins. I can put the text in the
> >>> pdf and it looks like it is trying to justify it but it won't wrap
to
> >>> the next line. I've looked at the documentation for both the
> >>> Text::Autoformat and PDF::API2 modules but can't seem to figure it
> >>> out.
> >>> I have 2 questions: 1. What am I doing wrong in that the text will
> >>> appear fine when I "print" it but that it won't appear correctly in
> >>> the pdf file? 2. Also, if the text is more than 1 page, how can I
get
> >>> it to automatically create a new page and continue onto the newly
> >>> created page?
> >>> My code:
> >>> #!/usr/bin/perl
> >>> use warnings;
> >>> use LWP::Simple;
> >>> use HTML::TokeParser;
> >>> use PDF::API2;
> >>> use Text::Autoformat;
> >>> # Print out the subtitle
> >>> my $oldtext =3D "trying to test if this sentence will be formatted
the=
> >>> correct way when it appears in the pdf file. For some reason I just
> >>> can't seem to get this to work. Well, maybe I can find help to get
> >>> this working. If I could get it to work it would really make my
kitty
> >>> purrrr";
> >>> my $newtext =3D autoformat $oldtext, { left=3D>8, right=3D>70,
justify=
=3D>
> >>> 'full' };
> >>> print $newtext;
> >>> #----create the pdf file----->
> >>> my $file =3D "This PDF";
> >>> my $pdf =3D PDF::API2->new( -file =3D> "$file.pdf" );
> >>> my $page =3D $pdf->page;
> >>> $page->mediabox ('A4');
> >>> $page->bleedbox(25,25,5,10);
> >>> $page->cropbox =A0(7.5,7.5,97.5,120.5);
> >>> my %font =3D (
> >>> =A0 =A0 =A0 Helvetica =3D> {
> >>> =A0 =A0 =A0 =A0 =A0 =A0Bold =A0 =3D> $pdf->corefont(
'Helvetica-Bold',=
=A0 =A0-encoding =3D>
> >>> 'latin1' ),
> >>> =A0 =A0 =A0 =A0 =A0 =A0Roman =A0=3D> $pdf->corefont( 'Helvetica',
=A0 =
=A0 =A0 =A0 -encoding =3D>
> >>> 'latin1' ),
> >>> =A0 =A0 =A0 =A0 =A0 =A0Italic =3D> $pdf->corefont(
'Helvetica-Oblique'=
, -encoding =3D>
> >>> 'latin1' ),
> >>> =A0 =A0 =A0 =A0},
> >>> =A0 =A0 =A0 =A0Times =3D> {
> >>> =A0 =A0 =A0 =A0 =A0 =A0Bold =A0 =3D> $pdf->corefont( 'Times-Bold',
=A0=
-encoding =3D>
> >>> 'latin1' ),
> >>> =A0 =A0 =A0 =A0 =A0 =A0Roman =A0=3D> $pdf->corefont( 'Times', =A0
=A0 =
=A0 =A0-encoding =3D>
> >>> 'latin1' ),
> >>> =A0 =A0 =A0 =A0 =A0 =A0Italic =3D> $pdf->corefont( 'Times-Italic',
-en=
coding =3D>
> >>> 'latin1' ),
> >>> =A0 =A0 =A0 =A0},
> >>> =A0 =A0);
> >>> my $main_text =3D $page->text;
> >>> $main_text->font( $font{'Times'}{'Roman'}, 2 );
> >>> $main_text->fillcolor('black');
> >>> $main_text->translate( 5, 100 );
> >>> $main_text->text("$newtext");
> >>> $pdf->save;
> >>> $pdf->end();
> >> Unfortunately PDF files aren't the easiest of things to create, but
the=
program
> >> below does what you want and should help you get started. Remember
that=
, unless
> >> you change from the default, all units are 1/72 of an inch; the
origin =
of the
> >> page is at the lower left corner and values for Y increase upwards.
>
> >> There is no point in preformatting the text as newlines are ignored,
an=
d the
> >> extra spaces will simply increase the distance between words.
>
> >> This program translates to a point 1in from the left and top edges of
a=
n A4 page
> >> and then adds the text as a paragraph in a box 2in high and 1in from
th=
e
> >> right-hand edge. Note also that I've set the font point size to 12
(you=
had 2
> >> before which is nearly invisible) and added 16pt leading (the
distance =
between
> >> the bases of the lines of text).
>
> >> HTH,
>
> >> Rob
>
> >> use strict;
> >> use warnings;
>
> >> use PDF::API2;
>
> >> my $oldtext =3D "trying to test if this sentence will be formatted
the
> >> correct way when it appears in the pdf file. For some reason I just
> >> can't seem to get this to work. Well, maybe I can find help to get
> >> this working. If I could get it to work it would really make my kitty
> >> purrrr";
>
> >> my $file =3D 'This PDF';
> >> my $pdf =3D PDF::API2->new( -file =3D> "$file.pdf" );
> >> my $page =3D $pdf->page;
>
> >> my $times =3D $pdf->corefont( 'Times', -encoding =3D> 'latin1');
>
> >> my $main_text =3D $page->text;
>
> >> $main_text->font($times, 12);
> >> $main_text->fillcolor('black');
> >> $main_text->lead(16);
> >> $main_text->translate(72, 700);
> >> $main_text->paragraph($oldtext, 450, 144);
>
> >> $pdf->save;
> >> $pdf->end;- Hide quoted text -
>
> >> - Show quoted text -
>
> > Thank you! Again, you guys are extremely helpful! The code works
> > perfectly.
>
> > I want each PDF file to have 38 lines of text per page. If the text is
> > over 38 lines, then I want to create a new page and put that text in
> > page 2. Again, if page 2 has more than 38 lines of text, then add a
> > 3rd page and so on and so forth until the end of the text that I have.
> > How would I do that in this situation?
>
> First of all I really should have coded
>
> =A0 my $page =3D $pdf->page;
> =A0 $page->mediabox('A4');
>
> Otherwise your page sizes will be something else - probably 8in x 11in.
>
> You really need to learn PDF to do be able to create PDF documents. Take
a=
look here
>
> =A0http://www.adobe.com/devnet/pdf/pdf_reference.html
>
> But in the mean time take a look at the call to paragraph
>
> =A0 $main_text->paragraph($oldtext, 450, 144);
>
> which flows the text into a 2in high rectangle. If the text won't all
fit =
then
> the method returns any excess characters, so you could write
>
> =A0 my $over =3D $main_text->paragraph($oldtext, 450, 36);
> =A0 print $over, "\n";
>
> which puts as much of the text as possible into a half-inch high
rectangle=
and
> print whatever remains to STDOUT. Try it and prove to yourself that it
wor=
ks.
>
> You need to do a lot of arithmetic to format the text as you want. First
d=
ecide
> on your font size - my 12pt font was a random guess. If you have only 38
l=
ines
> per page you may want something bigger.
>
> Then choose your leading - it's traditional to use about 20% over your
fon=
t
> size, so for a 12pt font 14pt leading is more appropriate that the 16pt
th=
at I
> coded.
>
> Finally, if you want 38 lines per page then the bounding rectangle for
you=
r
> paragraph should be 38 x leading.
>
> Above all, remember that what you are doing is hard, and you should
experi=
ment a
> lot and look for examples in the Web.
>
> HTH,
>
> Rob- Hide quoted text -
>
> - Show quoted text -
I've been reading through the modules and the link you provided but it
seems pretty involved. I don't think it should be very difficult: all
that I am trying to do is create a multi-page PDF file with text in
it. Isn't there an easier way to do that?


|