zaboti has asked for the wisdom of the Perl Monks concerning the following question:

Hi I have a problem with pdf::api2 I need to edit an existing pdf and put in some images. The problem is that for inserting 4 images it takes around 20 seconds per image. So the whole process goes up to minute and a half. Is there some magic i can do to speed up the process? The images are 1920 × 1080 and need to stay that size, because i need quality pdf-s... So without further ado, here is the code:
#!/usr/bin/perl use PDF::API2; print "start ".(localtime)."\n"; $pdf = PDF::API2->open("sample.pdf"); $page = $pdf->openpage(1); $page->mediabox(840,600) $gfx=$page->gfx; print "first image ".(localtime)."\n"; $first=$pdf->image_png("first.png"); print "inserting first image ".(localtime)."\n"; $gfx->image($first,134,106,510,281); print "saving ".(localtime)." \n"; $pdf->saveas('new_file.pdf'); print "done ".(localtime)." \n";
The output i get:
start Mon Jun 3 10:46:31 2013 first image Mon Jun 3 10:46:31 2013 inserting first image Mon Jun 3 10:46:53 2013 saving Mon Jun 3 10:46:53 2013 done Mon Jun 3 10:46:57 2013
So the most time consuming process is image_png which takes 22 seconds in this example... Any help would be appreciated. Thanks

Update: if i use the same image converted to JPEG, it works flawlessly, under a second. The problem is i need the transparency of the PNG files

Replies are listed 'Best First'.
Re: PDF::API2 image_png slow
by rpnoble419 (Pilgrim) on Jun 03, 2013 at 15:20 UTC

    have you tried to optimize the color usage in the PNG files to reduce their size? Also check the DPI of the PNG file, make sure its the same as the jpg file...