使用 Python 压缩 PDF 或减小 PDF 文件大小的 5 种方法
在存储、共享和传输方面,处理大型 PDF 文件可能是一个真正的挑战。PDF 压缩提供了一种有效的解决方案来减小文件大小,使文档更易于管理并优化存储使用情况。压缩的 PDF 文件具有几个关键优势:
- 降低存储成本:较小的文件大小意味着较低的长期存储要求和费用。
- 提高传输效率:减小的文件大小可以提高上传和下载速度,特别有利于通过电子邮件、云驱动器和其他渠道共享文件。
- 增强的用户体验:轻量级 PDF 文件的加载和显示速度更快,尤其是在移动设备上,同时还可以减少网络带宽消耗。
- 提高内容可访问性:紧凑的 PDF 文件更容易被搜索引擎索引和发现,有助于提高内容的可见性和覆盖范围。
用于压缩 PDF 文件的 Python 库
要在 Python 中压缩 PDF 文件,我们将使用 Spire.PDF for Python。它是一个功能丰富且用户友好的库,旨在在 Python 应用程序中创建、读取、编辑和转换 PDF 文件。
您可以使用以下 pip 命令从 PyPI 安装 Spire.PDF for Python:
pip install Spire.Pdf
如果您已经安装了 Spire.PDF for Python 并希望升级到最新版本,请使用以下 pip 命令:
pip install --upgrade Spire.Pdf
有关安装的更多详细信息,您可以查看此官方文档:如何在 VS Code 中安装 Spire.PDF for Python。
通过使用 Python 优化图像来压缩 PDF
PDF 文件通常包含大量高分辨率图像,这些图像可能是影响文件整体大小的主要因素。通过压缩这些图像并降低其分辨率,您可以显著减小 PDF 的文件大小。
Spire.PDF for Python 中的 PdfCompressor 类负责压缩 PDF 文件。通过使用
PdfCompressor.OptimizationOptions.SetImageQuality()、
PdfCompressor.OptimizationOptions.SetResizeImages() 和
PdfCompressor.OptimizationOptions.SetIsCompressImage() 方法,您可以通过优化其中包含的图像来轻松压缩 PDF 文件。
这是一个简单的示例,展示了如何通过使用 Python 优化图像来压缩 PDF:
from spire.pdf import *
from spire.pdf.common import *
# Create a PdfCompressor object and specify the path of the PDF file to be compressed
input_pdf = "Sample.pdf"
compressor = PdfCompressor(input_pdf)
# Configure the compression options to optimize images in the PDF
compression_options = compressor.OptimizationOptions
compression_options.SetImageQuality(ImageQuality.Medium)
compression_options.SetResizeImages(True)
compression_options.SetIsCompressImage(True)
# Compress the PDF file and save the result to a new file
output_pdf = "OptimizingImages.pdf"
compressor.CompressToFile(output_pdf)
使用 Python 压缩 PDF 中的图像
通过使用 Python 优化字体来压缩 PDF
PDF 中嵌入的字体也会导致文件大小变大。通过压缩或取消嵌入来优化这些字体有助于减小文件大小。
要压缩或取消嵌入 PDF 文件中的字体,您可以使用
PdfCompressor.OptimizationOptions.SetIsCompressFonts() 或
PdfCompressor.OptimizationOptions.SetIsUnembedFonts() 方法。
下面是一个简单的示例,演示如何通过使用 Python 压缩或取消嵌入字体来压缩 PDF:
from spire.pdf import *
from spire.pdf.common import *
# Create a PdfCompressor object and specify the path of the PDF file to be compressed
input_pdf = "Sample.pdf"
compressor = PdfCompressor(input_pdf)
# Configure the compression options to optimize fonts in the PDF
compression_options = compressor.OptimizationOptions
# Enable font compression
compression_options.SetIsCompressFonts(True)
# Or enable font unembedding
# compression_options.SetIsUnembedFonts(True)
# Compress the PDF file and save the result to a new file
output_pdf = "OptimizingFonts.pdf"
compressor.CompressToFile(output_pdf)
通过使用 Python 删除附件来压缩 PDF
PDF 文件有时可能包含附件,例如图像、文档或其他媒体。这些附加文件会显著增加 PDF 的整体大小。通过使用
PdfDocument.Attachments.Clear() 方法,您可以轻松地从 PDF 文件中删除所有附件。
这是一个简单的示例,展示了如何通过使用 Python 删除 PDF 中的所有附件来减小 PDF 的文件大小:
from spire.pdf import *
from spire.pdf.common import *
# Create a PdfDocument object and specify the path of the PDF file to be compressed
input_pdf = "Sample.pdf"
pdf = PdfDocument(input_pdf)
# Disable the incremental update
pdf.FileInfo.IncrementalUpdate = False
# Remove all attachments from the PDF
pdf.Attachments.Clear()
# Save the result to a new file
output_pdf = "RemovingAttachments.pdf"
pdf.SaveToFile(output_pdf)
pdf.Close()
通过使用 Python 删除或拼合表单域来压缩 PDF
与静态 PDF 文档相比,包含交互式表单域(如文本框、复选框或下拉菜单)的 PDF 文档往往具有更大的文件大小。这是因为表单域数据和关联的元数据单独存储在 PDF 文件中。通过完全删除表单域或拼合它们,您可以显著减小 PDF 的文件大小。
下面是一个简单的示例,演示如何通过使用 Python 拼合表单域来减小 PDF 的文件大小:
from spire.pdf import *
from spire.pdf.common import *
# Create a PdfDocument object and specify the path of the PDF file to be compressed
input_pdf = "Sample.pdf"
pdf = PdfDocument(input_pdf)
# Disable the incremental update
pdf.FileInfo.IncrementalUpdate = False
# Flatten the form fields in the PDF
pdf.Form.IsFlatten = True
# Save the result to a new file
output_pdf = "FlatteningForms.pdf"
pdf.SaveToFile(output_pdf)
pdf.Close()
下面是一个简单的示例,演示如何通过使用 Python 删除表单域来减小 PDF 的文件大小:
from spire.pdf import *
from spire.pdf.common import *
# Create a PdfDocument object and specify the path of the PDF file to be compressed
input_pdf = "Sample.pdf"
pdf = PdfDocument(input_pdf)
# Disable the incremental update
pdf.FileInfo.IncrementalUpdate = False
# Get the forms in the PDF
form = pdf.Form
formWidget = PdfFormWidget(form)
# Remove all forms from the PDF
formWidget.FieldsWidget.Clear()
# Save the result to a new file
output_pdf = "RemovingForms.pdf"
pdf.SaveToFile(output_pdf)
pdf.Close()
通过使用 Python 删除注释来压缩 PDF
PDF 文档可以包含各种类型的批注,如注释和高亮显示。这些批注在 PDF 文件中作为单独的对象存储,这可能会导致整体文件大小增加。通过从 PDF 中删除批注,您可以有效地减小文件大小,而不会影响文档的核心内容。
下面是一个简单的示例,演示如何通过使用 Python 删除批注来减小 PDF 的文件大小:
from spire.pdf import *
from spire.pdf.common import *
# Create a PdfDocument object and specify the path of the PDF file to be compressed
input_pdf = "Sample.pdf"
pdf = PdfDocument(input_pdf)
# Disable the incremental update
pdf.FileInfo.IncrementalUpdate = False
# Remove all annotations from the pages of the PDF
for i in range(pdf.Pages.Count):
page = pdf.Pages[i]
page.Annotations.Clear()
# Save the result to a new file
output_pdf = "RemovingAnnotations.pdf"
pdf.SaveToFile(output_pdf)
pdf.Close()