PDF-Writer icon indicating copy to clipboard operation
PDF-Writer copied to clipboard

Adding 'print' javascript to an existing PDF file

Open amitkanfer opened this issue 7 years ago • 3 comments

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

amitkanfer avatar Jul 26 '16 16:07 amitkanfer

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 [email protected] 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 .

KarlHegbloom avatar Jul 26 '16 17:07 KarlHegbloom

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

amitkanfer avatar Jul 26 '16 18:07 amitkanfer

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);
    });

galkahana avatar Jul 26 '16 21:07 galkahana