How to resize PDF via Cloud Python SDK
To resize 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 resize PDF via Python SDK
Aspose.PDF Cloud developers can easily load & resize PDF in just a few lines of code.
- Install Python SDK
- Upload original file to Aspose Cloud
- Set up path and parameters for conversion to HTML
- Convert PDF to HTML (XHTML format)
- Convert the resized HTML back to PDF
- Download the final resized PDF file
Resize PDF using Python
import os
import logging
from pathlib import Path
from asposepdfcloud import PdfApi, AsposeResponse, HtmlDocumentType, OutputFormat
from change_layout_helper import PdfChangeLayoutHelper
class PdfResizePages:
def __init__(self, pdf_api: PdfApi, helper: PdfChangeLayoutHelper):
self.pdfApi = pdf_api
self.helper = helper
def resizeAllPages(self, documentName: str, htmlTempDoc: str, width: int, height: int, outputDocumentName: str, localFolder: Path, tempFolder: str):
self.helper.uploadFile(documentName, localFolder, tempFolder)
htmlTempPath = os.path.join(tempFolder, htmlTempDoc)
args = {
"folder": tempFolder,
"document_type": HtmlDocumentType.XHTML,
"output_format": OutputFormat.FOLDER,
}
response: AsposeResponse = self.pdfApi.put_pdf_in_storage_to_html(documentName, htmlTempPath, **args)
if response.code != 200:
logging.error("resizeAllPages(): Unexpected error in PDF to HTML conversion!")
else:
logging.info("resizeAllPages(): Successfully convert PDF to HTML!")
args2 = {
"dst_folder": tempFolder,
"html_file_name": htmlTempDoc,
"height": height,
"width": width,
}
response: AsposeResponse = self.pdfApi.put_html_in_storage_to_pdf(outputDocumentName, htmlTempPath, **args2)
if response.code != 200:
logging.error("resizeAllPages(): Unexpected error in HTML to PDF conversion!")
else:
logging.info("resizeAllPages(): Successfully convert HTML tot PDF!")
self.helper.downloadFile(outputDocumentName, outputDocumentName, localFolder, tempFolder, "resized_")
With our Python library you can:
Resize 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.