Re: PDF mod suggestions
by BigLug (Chaplain) on Jun 24, 2004 at 02:44 UTC
|
use PDF::API2;
my $newpdf = new PDF::API2;
my $oldpdf = PDF::API2->open($filename);
for my $pg (1 .. $oldpdf->pages) {
my $img = $pdf->pdfimageobj( $oldpdf, $pg );
my $page = $pdfnew->page();
$page->mediabox( 6 * 72, 9 * 72); # There's 72 postscript points in
+ an inch
my $gfx = $page->gfx;
$gfx->pdfimage(
$img,
0.5 * 72, # half inch from the left
0.5 * 72, # half inch from the bottom
5 / 8.5, # x scale = new width / old width
8 / 11 # y scale = new height / old height
);
}
$pdf->saveas($newfilename);
$pdf->end();
(Code is untested but worked in my head. There shouldn't be much wrong with it. AFAIK, PDF::API2 will automatically embed any fonts it needs -- so long as it can find them)
"Get real! This is a discussion group, not a helpdesk. You post something, we discuss its implications. If the discussion happens to answer a question you've asked, that's incidental." -- nobull@mail.com in clpm
| [reply] [d/l] |
|
|
This is one of best examples for PDF::API2 I ever found. ++ However, it has few problems, so corrected version is below:
#!/usr/bin/perl -w
use strict;
use PDF::API2;
my $newpdf = new PDF::API2;
my $newfilename = my $filename = shift @ARGV || die "usage: $0 file.pd
+f";
$newfilename =~ s/(\.pdf)/-resized$1/i;
my $oldpdf = PDF::API2->open($filename);
for my $pg ( 1 .. $oldpdf->pages ) {
my $img = $newpdf->pdfimageobj( $oldpdf, $pg );
my $page = $newpdf->page();
$page->mediabox( 6 * 72, 9 * 72 ); # There's 72 postscript poin
+ts in +an inch
my $gfx = $page->gfx;
$gfx->pdfimage(
$img,
0.5 * 72, # half inch from the left
0.5 * 72, # half inch from the bottom
5 / 8.5, # x scale = new width / old width
8 / 11 # y scale = new height / old height
);
}
$newpdf->saveas($newfilename);
$oldpdf->end();
I can also confirm that it embeds fonts (and only subset of used ones).
Update: fixed code
| [reply] [d/l] |
|
|
Thanks for you comments and fixes .. however I can only see one thing that is changed (other than the addition of CLI code).
You've called pdfimageobj on the old pdf rather than the new one. That's wrong -- you need to call it on the new file, so that the page is loaded into the new pdf. The gfx->pdfimage call later then tells where that page should be placed.
You can tell that by the interface: You code ($img = $oldpdf->pdfimageobj($oldpdf, $pg)) would set @_ in the sub to ($oldpdf, $oldpdf, $pg). That wouldn't be right. There's be no need to declare the $oldpdf as a parameter to the sub as it's the object on which it's being called.
"Get real! This is a discussion group, not a helpdesk. You post something, we discuss its implications. If the discussion happens to answer a question you've asked, that's incidental." -- nobull@mail.com in clpm
| [reply] [d/l] [select] |
|
|
Re: PDF mod suggestions
by dragonchild (Archbishop) on Jun 24, 2004 at 12:27 UTC
|
Is there a reason you have to do this in Perl? Adobe (the creator of the PDF format) has excellent tools for just this process. ghostscript is also an excellent tool that I've used in similar situations.
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
I shouldn't have to say this, but any code, unless otherwise stated, is untested
| [reply] |
Re: PDF mod suggestions
by injunjoel (Priest) on Jun 24, 2004 at 01:51 UTC
|
| [reply] |
Re: PDF mod suggestions
by davidj (Priest) on Jun 24, 2004 at 01:13 UTC
|
| [reply] |
|
|
thats not much of a reccomendation (you don't even know if they can do the job)
| [reply] |
|
|
You are absolutely right. I DON'T know if they can do the job. The point of my post was that in less than a minute of a google search I was able to find at least 2 options that might be worth looking into.
But then again, I didn't know it was my job to do his job for him. Don't get me wrong: I'll be more than glad to go the extra mile for a fellow coder. Someone says, "you know, I have exhausted all my knowledge and all my resources, and now I need a little help." I'll be more than willing take my time, find/create a solution, and gladly share it with him. But someone who hasn't even taken the first step: I'm less willing.
I have only been a fellow Monk for about 6 weeks now, but I was under the impression that the purpose of this site is to get insight from fellow monks, to be given ideas, and to be given a direction in which to follow. If you are looking for someone to do your research for you, to write your code for you, and to solve your problem or do your homework for you, then I believe you have come to the wrong place.
But then again, I could be wrong :)
davidj
| [reply] |