Good evening!

I am attempting to merge several thousand small (<20KiB) PDF file together into one PDF using PDF::API2.

The merging script works correctly at the beginning, but quickly bogs down after processing ~100 PDFs before grinding to a halt with the following warnings:

Deep recursion on subroutine "PDF::API2::Basic::PDF::Objind::release" +at C:/Perl64/site/lib/PDF/API2/Basic/PDF/Objind.pm line 123. Deep recursion on subroutine "PDF::API2::Basic::PDF::Objind::release" +at C:/Perl64/site/lib/PDF/API2/Basic/PDF/Objind.pm line 123. Deep recursion on subroutine "PDF::API2::Basic::PDF::Objind::release" +at C:/Perl64/site/lib/PDF/API2/Basic/PDF/Objind.pm line 123. ...

Here is a small sample merging script that exhibits the problem
(the script used to generate the sample PDF files can be found in the READMORE section):

#!/usr/bin/perl use 5.018; use PDF::API2; use strict; use warnings; my $path = "./pdfs/"; my $out_pdf_file = 'merged.pdf'; my $out_pdf; opendir (my $DIR, $path) or die "Could not open $path:\n$!\n$^E"; chdir $path; while ( my $in_pdf_file = readdir $DIR ) { next if $in_pdf_file =~ /^\./; my $in_pdf = PDF::API2->open($in_pdf_file) or die "Error opening PDF + file [$in_pdf_file]:\n$!\n$^E"; # Append to output if PDF already exists if ( -e $out_pdf_file ) { $out_pdf = PDF::API2->open($out_pdf_file); } # Create new PDF if output does not exist else { $out_pdf = PDF::API2->new(-file => $out_pdf_file); } foreach my $page ( 1 .. $in_pdf->pages() ) { $out_pdf->import_page($in_pdf, $page, 0); } $out_pdf->update(); } closedir $DIR;

I cannot help but feel I am going about this merging process in a horribly inefficient way (Schlemiel the painter anyone?). Does anyone have any pointers toward a correct/more efficient merging technique?

Thank you for your time.

perl -v This is perl 5, version 18, subversion 2 (v5.18.2) built for MSWin32-x +64-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2013, Larry Wall Binary build 1802 [298023] provided by ActiveState http://www.ActiveSt +ate.com Built Apr 15 2014 10:38:37
perl -MPDF::API2 -E "say $PDF::API2::VERSION;" 2.023

Script used to generate the sample PDF files:


In reply to [SOLVED] Problem merging thousands of PDFs with PDF::API2: 'Deep recursion on subroutine "PDF::API2::Basic::PDF::Objind::release"' by ateague

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.