Python: AttributeError: 'Page' object has no attribute 'write_javascript'

206 Views Asked by At

I am learning python. trying embembed a image into pdf file. But not sure why got this error after running this file

Code is:

import fitz

def embed_js_in_pdf(pdf_file_path, js_code):
    doc = fitz.open(pdf_file_path)
    for page in doc:
        page.write_javascript(js_code)
    doc.save("modified.pdf")
    doc.close()

# Replace 'path/to/your/file.pdf' with the actual path to your PDF file
pdf_file_path = 'original.pdf'

# JavaScript code to be embedded in the PDF
js_code = '''
function openImageAfterDelay() {
    var img = new Image();
    img.src = 'hidden.jpg';
    img.onload = function() {
        document.body.innerHTML = '';
        document.body.appendChild(img);
    };
}

// Call the function after a delay of 3 seconds
setTimeout(openImageAfterDelay, 3000);

embed_js_in_pdf(pdf_file_path, js_code)
'''

Error:

  File "C:\practises\13.embed_executable\embed_executable.py", line 28, in <module>
    embed_js_in_pdf(pdf_file_path, js_code)
  File "C:\practises\13.embed_executable\embed_executable.py", line 6, in embed_js_in_pdf
    page.write_javascript(js_code)
    ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Page' object has no attribute 'write_javascript'

Please, give me solution of this error
AttributeError: 'Page' object has no attribute 'write_javascript'

Please, give a solution.

0

There are 0 best solutions below