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

At this node lots of folks spoke of diferent ways in which they were able to manipulate PDF's and overwrite them with user or database driven data.

In fact, I reported success in my own experiment in that realm. But now I have a pdf generated using scribus, which I need to overwrite with a small piece of unique data.

Will the source of a PDF file or how it is initially created going to determine whether I can add text on top of an existing design?

-- Hugh

UPDATE:

OK, so here is the code I'm working with, currently reads more or less:

#!/usr/bin/perl -w use strict; use warnings; use PDF::API2; my $TEMPLATE = '/path/to/scribus/generated/pdffile.pdf'; my $RESULT_PDF = '/path/to/script/generated/pdf_result_file.pdf'; my $INSTANCE = 'Print this code on form'; my $pdf = PDF::API2->open($TEMPLATE); my $page = $pdf->openpage('1'); my $text = $page->text(); my $font = $pdf->corefont('Helvetica'); # prepare text object $text->font($font,48); # Set font to Helvetica, 11pt $text->fillcolor("#222222"); # This is black $text->translate(173,660); # Text start location for Corp chk box $text->text("$INSTANCE"); # Print "X" at 173,660 $pdf->saveas($RESULT_PDF); $pdf->end; # print status message: print STDERR "Efforts to automate pdf generation still under developme +nt.\n"; print STDERR "For the moment, please do this manually. Thanks.\n";
But I also have commented out, an attempt to use PDF::Reuse, as well.

if( $lal && $lol ) { $life++; }

Replies are listed 'Best First'.
Re: Adding text to existing pdf file.
by almut (Canon) on Dec 16, 2007 at 13:59 UTC
    Will the source of a PDF file or how it is initially created going to determine whether I can add text on top of an existing design?

    In principle, yes, that's possible. I can think of at least a few conceivable reasons. For instance,

    • there are several PDF versions1 (current is 1.7), and the editing tool you're using might not be compatible with a particular feature the PDF file is making use of.
    • more basically, most (free) tools simply aren't implementing the entire set of PDF features from the spec. And sometimes, features are interdependent in the sense that if you can't correctly parse some structure to locate whatever object you're trying to modify, you're going to have a hard time manipulating it...
    • the existing document is incompliant in one way or another (sure, should not happen, but as we all know, software may have bugs, occasionally).
    • encryption/signing may lock down certain content or allowed actions (though that seems less likely in your case)
    • ... ...

    But I suspect that your question is not an end in itself... so, in order to elicit more useful replies, it would help if you provided some details, like

    • what exactly you are trying to do, i.e. adding/modifying ordinary content (like the node title suggests) or rather filling in forms (like what is primarily being discussed in the thread you linked to)?
    • some actual code, showing what tools (CAM::PDF, PDF::API2, etc.) you've tried in what ways to accomplish the editing.
    • make accessible somewhere a sample scribus-generated PDF file which exhibits the problem in combination with what you've tried.

    ___

    1  For some indication of the version, look at the first few bytes of the document, it says something like %PDF-1.6  — though the version therein does not necessarily correspond to the feature set actually required...  Many PDF creation tools don't go to the trouble of adjusting this header dynamically to hold the minimal version required, depending on which features are really being used in the document (similar in spirit to "use 5.006" in a Perl script), but rather just dump the version the tool itself is maximally supporting.

Re: Adding text to existing pdf file.
by almut (Canon) on Dec 16, 2007 at 21:31 UTC

    As to your update: without the input PDF it's unfortunately still hard to tell what the problem is.

    If you mail me a scribus sample PDF (together with a short description of what the expected output is), I'll see what I can do... (/msg me for my mail addy, if interested).