通过 Python 打印 PDF

如何使用 Python 库打印 PDF

如何使用 Python 库打印 PDF

为了打印 PDF 文件,我们将使用 Aspose.PDF for .NET API,这是一款适用于 python-net 平台的功能丰富、强大且易于使用的文档处理 API。打开 NuGet 软件包管理器,搜索 Aspose.pdf 然后安装。你也可以使用软件包管理器控制台中的以下命令。

Python Package Manager Console

pip install aspose-pdf

通过 Python 打印 PDF 文档


你需要 Aspose.PDF for .NET 在你的环境中试用代码。

1.使用 “文档” 实例加载 PDF。 1.使用 Document.Info 属性获取文档信息。 1.访问并显示不同的 Document.Info 属性。

打印 PDF-Python。

<% print.code-block.subtitle %>

    import aspose.pdf as ap
    import aspose.pydrawing as drawing

    # Create PdfViewer object
    viewer = ap.facades.PdfViewer()

    # Open input PDF file
    viewer.bind_pdf("input.pdf")

    # Set attributes for printing
    # Print the file with adjusted size
    viewer.auto_resize = True
    # Print the file with adjusted rotation
    viewer.auto_rotate = True
    # Do not produce the page number dialog when printing
    viewer.print_page_dialog = False

    # Create objects for printer and page settings
    ps = drawing.printing.PrinterSettings()
    pgs = drawing.printing.PageSettings()

    # Set XPS/PDF printer name
    ps.printer_name = "Microsoft XPS Document Writer"
    # Or set the PDF printer
    # ps.printer_name = "Adobe PDF"

    # Set PageSize(if required)
    pgs.paper_size = drawing.printing.PaperSize("A4", 827, 1169)

    # Set PageMargins(if required)
    pgs.margins = drawing.printing.Margins(0, 0, 0, 0)

    # Print document using printer and page settings
    viewer.print_document_with_settings(pgs, ps)

    # Close the PDF file after printing
    viewer.close()