PNG JPG BMP TIFF PDF
Aspose.PDF  for Python

Crop PDF in Python SDK

Crop PDF Document using Cloud Python SDK.

Get Started

How to crop PDF via Cloud Python SDK

To crop PDF, we’ll use Aspose.PDF Cloud Python SDK. This Cloud SDK assists Python programmers in developing cloud-based PDF creator, annotator, editor, and converter apps using Python programming language via Aspose.PDF REST API. If the python package is hosted on Github, you can install directly from Github:

Installation from Github


     
    pip install git+https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-python.git

Package Manager Console Command

     
    pip install asposepdfcloud

Steps to crop PDF via Python SDK

Aspose.PDF Cloud developers can easily load & crop PDF in just a few lines of code.

  1. Install Python SDK
  2. Upload original file to Aspose Cloud
  3. Fetch page metadata (optional, maybe used internally)
  4. Extract page as image with desired crop dimensions
  5. Create a new PDF with given width and height
  6. Insert the cropped page (image) into the new document
  7. Download and save the document locally
 

Crop PDF using Python


    import os
    import logging
    from pathlib import Path
    from asposepdfcloud import PdfApi, AsposeResponse, DocumentResponse
    from change_layout_helper import PdfChangeLayoutHelper, Config

    class PdfCropPages:
        def __init__(self, pdf_api: PdfApi, helper: PdfChangeLayoutHelper):
            self.pdfApi = pdf_api
            self.helper = helper

        def cropDocumentPage(self, documentName: str, pageNumber: int, llx: int, lly: int, width: int, height: int, outputDocument: str, localFolder: str, tempFolder: str):
            self.helper.uploadFile(documentName, localFolder, tempFolder)

            self.helper.getPageInfo(documentName, pageNumber, tempFolder)

            imageFile = self.helper.extractPdfPage(documentName, pageNumber, Config.CROP_PAGE_WIDTH, Config.CROP_PAGE_HEIGHT, localFolder, tempFolder)
            newPdf: DocumentResponse = self.helper.createDocument(outputDocument, width, height, tempFolder)
            if newPdf.code != 200:
                logging.error("cropDocumentPage(): Failed to create new PDF document!")
            else:
                response: AsposeResponse = self.helper.insertPageAsImage(outputDocument, imageFile, llx, lly, tempFolder)
                if response.code != 200:
                    logging.error("cropDocumentPage(): Can't crop pdf document page!")
                else:
                    logging.info("cropDocumentPage(): Page successfully cropped.")
                    self.helper.downloadFile(outputDocument, outputDocument, localFolder, tempFolder, "cropped_")
 

With our Python library you can:

Crop PDF documents with Aspose.PDF Cloud Python SDK.

  • Combine PDF documents.
  • Split PDF Files.
  • Convert PDF to other formats, and vice versa.
  • Manipulate Annotations.
  • Work with Images in PDF, etc.
  • You can try out our free App to test the functionality online.