How to secure PDF documents in python using GroupDocs.Viewer REST API?

The GroupDocs.Viewer REST API is a platform independent document viewer and rendering solution. It allows to secure output PDF documents from unauthorized access by setting permissions and passwords for opening or changing permissions.

One can use GroupDocs.Viewer REST API on any platform/language that supports REST. However, for the developers’ convenience, we have provided SDKs for almost all popular programming languages that include Python, .NET, JAVA, Node.js, PHP, Ruby and Android. As stated above, in this post we will focus on GoupDocs.Viewer Cloud SDK for Python.

Here we go. We will demonstrate how you can protect PDF with a password and set permissions using Python. We are using GroupDocs Cloud default internal storage in the following example. You can use other 3rd party cloud storage as per your need.

First of all, you need to sign up to groupdocs.cloud and get the credentials and then use the following Python script to secure the PDF document.

# Import module
import groupdocs_viewer_cloud

from shutil import copyfile

# Get your app_sid and app_key at https://dashboard.groupdocs.cloud (free registration is required).
app_sid = "xxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxx"
app_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# Create instance of the API
viewer_api = groupdocs_viewer_cloud.ViewApi.from_keys(app_sid, app_key)
file_api = groupdocs_viewer_cloud.FileApi.from_keys(app_sid, app_key)

try:
        #upload soruce file to storage
        filename = 'C:/Temp/02_pages.pdf'
        remote_name = '02_pages.pdf'

        request_upload = groupdocs_viewer_cloud.UploadFileRequest(remote_name,filename)
        response_upload = file_api.upload_file(request_upload)

        #Render PDF with password
        view_options = groupdocs_viewer_cloud.ViewOptions()
        view_options.file_info = groupdocs_viewer_cloud.FileInfo()
        view_options.file_info.file_path = remote_name
        view_options.output_path= "protected"
        view_options.view_format = "PDF"
        view_options.render_options = groupdocs_viewer_cloud.PdfOptions()
        view_options.render_options.permissions = "DenyModification"
        view_options.render_options.permissions_password = "p123"
        view_options.render_options.document_open_password = "o123"
        
        request = groupdocs_viewer_cloud.CreateViewRequest(view_options)
        response = viewer_api.create_view(request)
        print("Document processed successfully: " + str(response))

        #download file from storage to local drive
        request_download = groupdocs_viewer_cloud.DownloadFileRequest('protected/02_pages_pdf/02_pages.pdf')
        response_download = file_api.download_file(request_download)
        copyfile(response_download, 'C:/Temp/02_pages_protected.pdf')
        
except groupdocs_viewer_cloud.ApiException as e:
        print("Exception when calling get_supported_conversion_types: {0}".format(e.message))

The output PDF document is password protected and modification allowed with the permission(owner) password.

The possible values of the permissions parameter are as follows:

  • AllowAll

  • DenyPrinting

  • DenyModification

  • DenyDataExtraction

  • DenyAll

Please note in addition to PDF GroupDocs.Viewer REST API renders almost all popular business file formats like Open Office, DOCX , PPTX, XLSX etc. into JPEG, PNG, HTML, or PDF formats to view these documents in your application. Please don’t hesitate to ask any questions or concerns.