Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adobe Acrobat API in Python

System:

Python 3.6
Windows 10

Goal:

Use the Adobe Acrobat API to use the "Save As" feature to save a pdf to jpegs.

Note: For my purposes I cannot use Wand or other packages.

Resources:

Adobe_API_Documentation

Implementation_Example 1

Error_Handling_Issue

VBA_Example

Current Code:

import winerror
import win32com
from win32com.client.dynamic import Dispatch, ERRORS_BAD_CONTEXT

ERRORS_BAD_CONTEXT.append(winerror.E_NOTIMPL)

my_dir = r"path\\to\\example\\"
my_pdf = "example.pdf"

os.chdir(my_dir)
src = os.path.abspath(my_pdf)

pdDoc = Dispatch("AcroExch.PDDoc")
pdDoc.Open(src)

jsObject = pdDoc.GetJSObject()

jsObject.SaveAs(os.path.abspath('./output_example.jpeg'), "com.adobe.acrobat.jpeg")

Issue:

jsObject is Null

Resulting in the following traceback:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-26-9c82c454eb2a> in <module>()
----> 1 jsObject.SaveAs(os.path.abspath('./output_example.jpeg'), "com.adobe.acrobat.jpeg")

AttributeError: 'NoneType' object has no attribute 'SaveAs'

Error Documentation Notes:

GetJSObject
Gets a dual interface to the JavaScript object associated with the PDDoc. This allows automation clients full access to both built-in and user-defined JavaScript methods available in the document. For more information on working with JavaScript, see Developing Applications Using Interapplication Communication.

Syntax
LDispatch* GetJSObject();

Returns
The interface to the JavaScript object if the call succeeded, NULL otherwise.
like image 933
Schalton Avatar asked Jul 26 '18 02:07

Schalton


People also ask

Does Adobe Acrobat have an API?

The API offers several options to customize reading, annotating and downloading PDFs. The API will also provide insights on how PDFs are consumed with out-of-the-box integration with Adobe Analytics.

Is Adobe API free?

Adobe PDF Embed API Standardize your end-user viewing experiences with interactivity and analytics, all at no charge! Unlimited access at no charge.

Can you code in Adobe Acrobat?

You can tie Acrobat JavaScript code to a specific PDF document, a page, field, or button within that document, or a field or button within the PDF file, and even to a user action. JavaScript is useful for XML forms.


1 Answers

Consider interfacing with the AvDoc object as one of your links show its usage, and then build pdDoc and jsObject from it. Be sure to also wrap process in a try/except/finally block to effectively release COM objects regardless of error.

import os    
import winerror
from win32com.client.dynamic import Dispatch, ERRORS_BAD_CONTEXT

ERRORS_BAD_CONTEXT.append(winerror.E_NOTIMPL)

my_dir = r"C:\\path\\to\\example\\"
my_pdf = "example.pdf"

os.chdir(my_dir)
src = os.path.abspath(my_pdf)

try:
    AvDoc = Dispatch("AcroExch.AVDoc")    

    if AvDoc.Open(src, ""):            
        pdDoc = AvDoc.GetPDDoc()
        jsObject = pdDoc.GetJSObject()
        jsObject.SaveAs(os.path.join(my_dir, 'output_example.jpeg'), "com.adobe.acrobat.jpeg")

except Exception as e:
    print(str(e))

finally:        
    AvDoc.Close(True)

    jsObject = None
    pdDoc = None
    AvDoc = None
like image 118
Parfait Avatar answered Sep 23 '22 02:09

Parfait