galkahana / PDF-Writer

High performance library for creating, modiyfing and parsing PDF files in C++
http://www.pdfhummus.com
Apache License 2.0
896 stars 214 forks source link

Adding 'print' javascript to an existing PDF file #55

Closed amitkanfer closed 5 months ago

amitkanfer commented 8 years ago

Hi Gal, Very nice project, thanks!

Can you please guide me what's the best and easiest way to make an existing PDF file to be 'auto-printed' when it's downloaded to a browser?

Thanks a lot!

Amit

KarlHegbloom commented 8 years ago

You should not auto print onto paper. It's their paper, not yours. Let the person downloading decide whether to print or to read it in a pdf reader.

On Tue, Jul 26, 2016, 10:45 amitkanfer notifications@github.com wrote:

Hi Gal, Very nice project, thanks!

Can you please guide me what's the best and easiest way to make an existing PDF file to be 'auto-printed' when it's downloaded to a browser?

Thanks a lot!

Amit

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/galkahana/PDF-Writer/issues/55, or mute the thread https://github.com/notifications/unsubscribe-auth/AACslhEzudnjvScFC1CemX0cRXSymkwEks5qZjmogaJpZM4JVWjw .

amitkanfer commented 8 years ago

:) Seriously, Let me rephrase - automatically open the print dialog.

galkahana commented 8 years ago

Nice one.
Did some looking around and it turns out that you can actually run javascript code as the document opens. and running something like this.print loads the dialog. at least when opened in acrobat...you can try other envs.

package.zip

the key things to do are:

  1. Create Javascript Action object with "this.print()" as its command
  2. Attach this action object to the document root (a.k.a catalog) as its "OpenAction". This is done by creating modified version of the document root with this "OpenAction" key added.

It's just as possible to do so also with new documents, and not modified ones. In that case step 1 remains the same. Step 2 is changed to simply adding the key while hummus writes the document root by listeting to the OnCatalogWrite event like this:

    pdfWriter.getEvents().on('OnCatalogWrite',function(params) {
        params.catalogDictionaryContext
            .writeKey('OpenAction')
            .writeObjectReferenceValue(actionObjectID);
    });