
打印 PDF 文档是应用程序开发中的一个常见要求。无论是生成报告、发票还是其他任何类型的文档,从代码中直接打印 PDF 文件到打印机都是一项非常有价值的功能。在本文中,我们将学习如何在 Python 中将 PDF 文件打印到打印机。
本文涵盖以下主题:
将 PDF 打印到打印机的 Python 库
用 Python 打印 PDF 文件
批量打印 PDF 文件
打印 PDF 的特定页面
指定打印 PDF 的页面大小
打印加密的 PDF 文件
在 Python 中将 PDF 转换为灰度并打印
授权许可
将 PDF 打印到打印机的 Python 库
要在 Python 中以编程方式打印 PDF 文件,我们将使用 Aspose.PDF for Python via .NET 库。通过简单的安装和使用,它为使用 Python 代码将 PDF 文件发送到打印机提供了高效的解决方案。Aspose.PDF for Python 允许开发人员在其 Python 应用程序中生成、处理和转换 PDF 文档。
要开始使用 Python 打印 PDF 文档,我们首先需要安装必要的库。请在控制台中使用以下 pip 命令从 PyPI 下载软件包或安装 API:
> pip install aspose-pdf
用 Python 打印 PDF 文件
通过以下步骤,我们可以用 Python 编程轻松打印 PDF 文件:
创建一个 PdfViewer 类的实例。
使用 bind_pdf() 方法加载输入的 PDF 文档。
然后,调用 print_document() 方法打印 PDF 文件。
最后,使用 close() 方法关闭 PDF 查看器。
下面的代码示例展示了如何在 Python 中打印 PDF 文件。
# This code example demonstrates how to print a PDF file in Python.
import aspose.pdf as ap
# Create PdfViewer object
viewer = ap.facades.PdfViewer();
# Open input PDF file
viewer.bind_pdf("Document.pdf");
# Print a PDF document
viewer.print_document();
# Close PDF file
viewer.close();用 Python 打印多个 PDF 文件
我们还可以通过以下步骤在 Python 中批量打印文件夹中的 PDF 文件:
从提供的文件夹路径加载 PDF 文件。
循环浏览所有 PDF 文件。
创建 PdfViewer 类的实例。
使用 bind_pdf() 方法逐个绑定每个输入的 PDF 文件。
然后,调用 print_document() 方法打印 PDF 文件。
最后,使用 close() 方法关闭 PDF 查看器。
下面的代码示例展示了如何在 Python 中打印多个 PDF 文件。
# This code example demonstrates how to print multiple PDF files at once in Python.
import os
import aspose.pdf as ap
# Directory path containing PDF files to print
path = "D:\\Files\\"
# Get PDF files
files = [f for f in os.listdir(path) if f.endswith(".pdf")]
# Read all files and print
for file in files:
# Create PdfViewer object
viewer = ap.facades.PdfViewer();
# Open input PDF file
viewer.bind_pdf(path + file);
# Print a PDF document
viewer.print_document();
# Close PDF file
viewer.close();用 Python 打印 PDF 的特定页面
在某些情况下,我们可能需要从 PDF 文档中打印特定范围的页面。为此,我们需要指定起始页和终止页的页码。我们可以通过以下步骤打印页面范围:
创建一个 PdfViewer 类的实例。
使用 bind_pdf() 方法加载输入的 PDF 文档。
可选择指定各种查看器属性,如自动调整大小、自动旋转等。
创建 PageSettings 类的实例。
初始化 PrinterSettings 类对象。
指定 print_range、from_page 和 to_page 属性。
然后,调用 viewer.print_document_with_settings() 方法,使用打印机和页面设置打印文档。
最后,使用 close() 方法关闭 PDF 查看器。
下面的代码示例展示了如何在 Python 中打印 PDF 文档的特定页面。
# This code example demonstrates how to print a range of pages from a PDF file in Python.
import aspose.pdf as ap
# Create PdfViewer object
viewer = ap.facades.PdfViewer();
# Open input PDF file
viewer.bind_pdf("Document.pdf");
# Set attributes for printing
viewer.auto_resize = True
viewer.auto_rotate = True
viewer.print_page_dialog = False
# Create objects for printer and page settings and PrintDocument
pgs = ap.printing.PageSettings();
ps = ap.printing.PrinterSettings();
# Set printer name
ps.printer_name = "Microsoft Print to PDF";
ps.print_range = ap.printing.PrintRange.SOME_PAGES;
ps.from_page = 1;
ps.to_page = 2;
# Print document using printer and page settings
viewer.print_document_with_settings(pgs, ps);
# Close PDF file
viewer.close();指定打印 PDF 的页面大小
同样,我们也可以按照前面提到的步骤,在打印 PDF 文件时指定页面大小。不过,我们只需在第 6 步使用 PaperSize 类指定纸张大小,并使用 Margins 类指定页边距。
下面的代码示例展示了如何使用 Python 以特定的页面大小和页边距打印 PDF 文档。
# This code example demonstrates how to print a range of pages from a PDF file in Python.
import aspose.pdf as ap
# Create PdfViewer object
viewer = ap.facades.PdfViewer();
# Open input PDF file
viewer.bind_pdf("Document.pdf");
# Set attributes for printing
viewer.auto_resize = True
viewer.auto_rotate = True
viewer.print_page_dialog = False
# Create objects for printer and page settings and PrintDocument
pgs = ap.printing.PageSettings();
ps = ap.printing.PrinterSettings();
# Set printer name
ps.printer_name = "Microsoft Print to PDF";
ps.print_range = ap.printing.PrintRange.SOME_PAGES;
ps.from_page = 1;
ps.to_page = 2;
# Set PageSize (if required)
pgs.paper_size = ap.printing.PaperSize("A4", 827, 1169);
# Set PageMargins (if required)
pgs.margins = ap.devices.Margins(0, 0, 0, 0);
# Print document using printer and page settings
viewer.print_document_with_settings(pgs, ps);
# Close PDF file
viewer.close();用 Python 打印加密的 PDF 文件
我们可以通过以下步骤打印加密或密码保护的 PDF 文件:
使用 Document 类加载带有密码的加密 PDF 文件。
创建一个 PdfViewer 类的实例。
使用 bind_pdf() 方法绑定 PDF 文档。
然后,调用 print_document() 方法打印 PDF 文件。
最后,使用 close() 方法关闭 PDF 查看器。
下面的代码示例展示了如何在 Python 中打印安全的 PDF 文件。
# Load secure PDF document while specifying User or Owner password
document = ap.Document("Password.pdf" , "userORowner");
# Create PdfViewer object
viewer = ap.facades.PdfViewer();
# Open input PDF file
viewer.bind_pdf(document);
# Print PDF document
viewer.print_document();
# Close PDF file
viewer.close();用 Python 将 PDF 转换为灰度并打印
我们可以通过以下步骤灰度打印 PDF 文档:
使用文档类加载输入的 PDF 文档。
初始化 RgbToDeviceGrayConversionStrategy 类对象。
循环浏览所有页面,并使用 strategy.convert() 方法转换为灰度图像。
创建 PdfViewer 类实例。
使用 bind_pdf() 方法绑定 PDF 文档。
然后,调用 print_document() 方法打印 PDF 文件。
最后,使用 close() 方法关闭 PDF 查看器。
下面的代码示例展示了如何使用 Python 将 PDF 转换为灰度文件并打印出来。
# This code example demonstrates how to print a PDF file as Grayscale in Python.
import aspose.pdf as ap
# Load the input PDF document
document = ap.Document("D:\\Files\\Output.pdf");
# Initiate RGB to Device Gry conversion strategy
strategy = ap.RgbToDeviceGrayConversionStrategy();
# Loop through all the pages
for page in document.pages:
# Convert the RGB colorspace image to GrayScale colorspace
strategy.convert(page);
# Create PdfViewer object
viewer = ap.facades.PdfViewer();
# Open input PDF file
viewer.bind_pdf(document);
# Print PDF document
viewer.print_document();
# Close PDF file
viewer.close();获取免费许可证
您可以获得临时许可证,以便在没有评估限制的情况下使用程序库。
结论
在本文中,我们学习了如何在 Python 中打印 PDF 文件。利用 Aspose.PDF for Python,您可以在 Python 代码中轻松地将 PDF 文件发送到打印机。这一强大的功能使用户能够跨不同的应用程序无缝地生成报告和管理文档工作流。按照本博文中概述的步骤,您可以自动完成打印 PDF 的任务。如有任何疑问,请随时联系我们。
渝公网安备50010702505508