jesparza / peepdf

Powerful Python tool to analyze PDF documents
http://peepdf.eternal-todo.com
GNU General Public License v3.0
1.32k stars 242 forks source link

Error: ReferenceError: print is not defined #58

Closed chesarin closed 7 years ago

chesarin commented 8 years ago

I installed pyv8 from https://github.com/buffer/pyv8. The installation of pyv8 succeeded; however, running the following test fails to interpret the javascript code in the variable jscode:

PPDF> set jscode "var a = 8; a = a + 2; print('The content of the variable is '+a);"
PPDF> js_eval variable jscode

*** Error: ReferenceError: print is not defined (  @ 1 : 22 )  -> var a = 8; a = a + 2; print('The content of the variable is '+a);

I get similar messages for PDFs that contain javascript and where I execute js_eval.

I ran this test on debian 7.11 and on remnux 6 with the latest version of peepdf found on github. I would appreciate if you can let me know if there is anything else I am missing in order to get peepdf to interpret javascript code.

jesparza commented 7 years ago

Hi @chesarin,

The idea behind PyV8, which is not part of peepdf, is that you can execute Javascript code from Python. However, you cannot use some of the functions that you would use in a Javascript console like the "print" function. This is something related to PyV8 but not to peepdf. If you want to know the value of the "a" variable in that case you can get the value of that variable with the "js_vars" command, for instance, you don´t really need the "print" function.

PPDF> set jscode "var a = 8; a = a + 2;"
PPDF> js_vars a

10

Sometimes when a PDF document contains JS code it can fail because it contains specific JS functions only present in the Acrobat Reader JS API. In that case, the best way to work with that is extracting the JS code and trying to replace the function with the value that the function would return. There is nothing like that implemented in peepdf yet, but it is something it might be included in the future.

I hope this helps!