Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing javascript print code embedded in pdf

Tags:

javascript

pdf

I have a server that generates a PDF, I have no access or ability to change the settings on the server.

When the server produces the pdf it embeds the following javascript code into the file so that when any PDF reader/viewer opens it, the PRINT DOCUMENT screen automatically opens. This is very inconvenient and frustrating.

The code inside the file at the very start looks like this:

%PDF-1.4 %âãÏÓ 1 0 obj <</S/JavaScript/JS(this.print\(true , 0,this.numPages-1,false\);\r)>> endobj 3 0 obj <</Length 10/Filter/FlateDecode>>stream xœ+ä SNIP

I thought it would be an easy task just to remove the javascript line and prevent the auto print screen from popping up.

I have tried this (just did a string search and replace and removed line 4). This DOES stop the print screen appearing - BUT when opened in a few PDF viewers (goodreader etc) instantly flags up as a corrupted PDF.

I can click the repair option and everything works fine, but I would like to know, is there anything I could do to replace the javascript code with some sort of NOOP code to keep the file from being corrupt whilst still preventing the print page?

Here's a link to a source file: https://www.dropbox.com/s/kziy6evi57cfhb3/2014-04-04_EIKY.pdf (800k)

Is there a way to nullify a pdf object or something similar?

Thank you.

like image 628
PilotSnipes Avatar asked Apr 04 '14 15:04

PilotSnipes


People also ask

How do I remove JavaScript from a PDF?

Choose Edit > Preferences (Windows) or Acrobat / Acrobat Reader > Preferences (Mac OS). From the Categories on the left, select JavaScript. In the JavaScript Security panel, set options to manage JavaScript: as needed. Uncheck to disable JavaScript completely or restrict JavaScript through APIs.

How do I remove embedded items from a PDF?

To simply remove an embedded file or object, select it and press Delete. To verify the issue is resolved, click File > Info > Check for Issues, and click Inspect Document.

How do I remove scripting and navigation from a PDF?

There are a number of ways you can approach that: Use “Tools>Edit PDF>Edit” and select the content in question, then press the Delete key. Use the “Contents” navigation pane (View>Show/Hide>Navigation Panes>Content), then find the content element in the tree and hit the Delete key.


1 Answers

Since PDF has checks to make sure that the content length hasn't changed at certain points, you can't add or remove characters. But you can change them. You can change it like this:

<</S/JavaScript/JS(this.print\(true , 0,this.numPages-1,false\);\r)>>

to this

<</S/JavaScript/JS(;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r)>>

for example.

like image 186
Joe Frambach Avatar answered Sep 28 '22 17:09

Joe Frambach