PNG
JPG
BMP
TIFF
PDF
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.
- Install Python SDK
- Upload original file to Aspose Cloud
- Fetch page metadata (optional, maybe used internally)
- Extract page as image with desired crop dimensions
- Create a new PDF with given width and height
- Insert the cropped page (image) into the new document
- 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.