in reply to Re: PDF mod suggestions
in thread PDF mod suggestions

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
2share!2flame...

Replies are listed 'Best First'.
Re^3: PDF mod suggestions
by BigLug (Chaplain) on Jun 25, 2004 at 03:46 UTC
    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
      Thanks, I'm still beginner in PDF::API2 (like so many othters) so that's why I appriciate examples like the one you provided.
      2share!2flame...