PNG JPG BMP TIFF PDF
Aspose.PDF  for Python

在 Python SDK 中调整 PDF 大小

使用云端 Python SDK 调整 PDF 文档大小。

Get Started

如何通过云端 Python SDK 调整 PDF 大小

要调整 PDF 大小,我们将使用 Aspose.PDF Cloud Python SDK。此云端 SDK 协助 Python 程序员使用 Python 编程语言通过 Aspose.PDF REST API 开发基于云的 PDF 创建、注释、编辑和转换应用程序。如果 Python 包托管在 Github 上,您可以直接从 Github 安装:

从 Github 安装


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

包管理器控制台命令

     
    pip install asposepdfcloud

通过 Python SDK 调整 PDF 大小的步骤

Aspose.PDF Cloud 开发人员可以轻松地在几行代码中加载和调整 PDF 大小。

  1. 安装 Python SDK
  2. 将原始文件上传到 Aspose Cloud
  3. 设置路径和参数以转换为 HTML
  4. 将 PDF 转换为 HTML(XHTML 格式)
  5. 将调整大小后的 HTML 转换回 PDF
  6. 下载最终调整大小的 PDF 文件
 

使用 Python 调整 PDF 大小


    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_")
 

通过我们的 Python 库您可以:

使用 Aspose.PDF Cloud Python SDK 调整 PDF 文档大小。

  • 合并 PDF 文档。
  • 拆分 PDF 文件。
  • 将 PDF 转换为其他格式,反之亦然。
  • 操作注释。
  • 处理 PDF 中的图像等。
  • 您可以尝试我们的 免费应用程序 在线测试功能。