PilotSnipes
PilotSnipes

Reputation: 307

Removing javascript print code embedded in 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.

Upvotes: 9

Views: 24990

Answers (4)

The easy way:

  1. Open the file with notepad++ or a similar editor.
  2. find the javascript code that triggers the print dialogue. You can use the find dialogue of the editor (ctl+f) and use the string "this.print".The rest of the code might change from document to document.
  3. Select all the characters inside the brackets of the JS instruction and count the number of characters. e.g.

    /JS (this.print({bUI:true,bSilent:false,bShrinkToFit:true});)

    See attached pic 1

  4. Replace all content inside the brackets for an exact number of semicolons. e.g.

    /JS (;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;)

  5. Save the document.

Upvotes: 6

PilotSnipes
PilotSnipes

Reputation: 307

@SakthiSureshAnand was looking for my code/library I used. It really is nothing special, but I thought I'd leave it here.

A simple php script requests the original file and then we get the contents of the file as a string:

Then preg_replace is all I use to replace the unwanted printing code and write the admended file to disk.

$fileString = file_get_contents('source.pdf');

$pdf = preg_replace(
  '%(<</S/Javascript/JS\()(.*;)(.*)%i',
  '<</S/Javascript/JS(;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r)>>',
  $fileString
);


//Do what you want with the fixed $pdf string.

Hope that helps someone.

Upvotes: 3

Mathieu Longtin
Mathieu Longtin

Reputation: 16700

With Foxit Reader on Windows, you can print the document to PDF, and the resulting PDF no longer has the Javascript actions.

Upvotes: 1

000
000

Reputation: 27227

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.

Upvotes: 15

Related Questions