pdftk 压缩选项

发布于 2024-10-21 12:52:26 字数 247 浏览 9 评论 0原文

我使用 pdftk 使用以下命令行压缩 pdf

pdftk file1.pdf output file2.pdf compress

它随着我的文件重量的减少而工作。

有[选项]来改变压缩吗???

或者也许有其他解决方案来压缩我的文件?它很重,因为某些图形有很多点。有没有办法将这些图形转换为 jpg 并调整压缩?

I use pdftk to compress a pdf using the following command line

pdftk file1.pdf output file2.pdf compress

It works as the weight of my file decreased.

Are there [options] to change the compression???

Or maybe other solutions to compress my file? It is heavy because some graphics have a lot of points. Is there a way to convert these graphs to jpg for instance and adapt the compression?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

腹黑女流氓 2024-10-28 12:52:26

我遇到了同样的问题,并找到了两种不同的解决方案(有关更多详细信息,请参阅此线程)。两者都极大地减小了我未压缩的 PDF 的大小。

  • 像素化(有损):

    转换输入.pdf -compress Zip 输出.pdf
    
  • 未像素化(无损,但显示可能略有不同):

    gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dBATCH -dQUIET -sOutputFile=output.pdf input.pdf
    

编辑:我刚刚发现了另一个选项(用于无损压缩),它避免了令人讨厌的 gs 命令。 qpdf 是一个简洁的工具,可以转换 PDF(压缩/解压缩、加密/解密),并且比 gs 命令快得多:

qpdf --linearize input.pdf output.pdf

I had the same problem and found two different solutions (see this thread for more details). Both reduced the size of my uncompressed PDF dramatically.

  • Pixelated (lossy):

    convert input.pdf -compress Zip output.pdf
    
  • Unpixelated (lossless, but may display slightly differently):

    gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dBATCH  -dQUIET -sOutputFile=output.pdf input.pdf
    

Edit: I just discovered another option (for lossless compression), which avoids the nasty gs command. qpdf is a neat tool that converts PDFs (compression/decompression, encryption/decryption), and is much faster than the gs command:

qpdf --linearize input.pdf output.pdf
傻比既视感 2024-10-28 12:52:26

尝试使用 PackBits 压缩、使用 Zip/Deflate 压缩的 tiff2pdf 来压缩我用 400ppi tiff 制作的 PDF(大部分为 8 位,少数为 24 位)。我在使用这些方法中的每一种时都遇到一个问题:上述方法都没有保留我在 Acrobat Pro X 中精心手动创建的书签目录。甚至连推荐的 ebook 设置为 gs.当然,我可以打开目录完好无损的原始副本并执行替换页面,但不幸的是,这些方法一开始都没有达到令人满意的效果。要么他们将尺寸缩小得太多,以至于质量像素化得令人无法接受,要么他们根本没有缩小尺寸,并且在一种情况下,尽管质量有所损失,但实际上却增加了尺寸。

pdftk 压缩:

no change in size
bookmarks TOC are gone

gs 屏幕:

takes a ridiculously long time and 100% CPU
errors:
    sfopen: gs_parse_file_name failed.                                 ? 
    | ./base/gsicc_manage.c:1651: gsicc_set_device_profile(): cannot find device profile
74.8MB-->10.2MB hideously pixellated
bookmarks TOC are gone

gs 打印机:

takes a ridiculously long time and 100% CPU
no errors
74.8MB-->66.1MB
light blue background on pages 1-4
bookmarks TOC are gone

gs 电子书:

errors:
    sfopen: gs_parse_file_name failed.
      ./base/gsicc_manage.c:1050: gsicc_open_search(): Could not find default_rgb.ic 
    | ./base/gsicc_manage.c:1651: gsicc_set_device_profile(): cannot find device profile
74.8MB-->32.2MB
badly pixellated
bookmarks TOC are gone

qpdf --linearize >:

very fast, a few seconds
no size change
bookmarks TOC are gone

pdf2ps:

took very long time
output_pdf2ps.ps 74.8MB-->331.6MB

ps2pdf:

pretty fast
74.8MB-->79MB
very slightly degraded with sl. bluish background
bookmarks TOC are gone

Trying to compress a PDF I made with 400ppi tiffs, mostly 8-bit, a few 24-bit, with PackBits compression, using tiff2pdf compressed with Zip/Deflate. One problem I had with every one of these methods: none of the above methods preserved the bookmarks TOC that I painstakingly manually created in Acrobat Pro X. Not even the recommended ebook setting for gs. Sure, I could just open a copy of the original with the TOC intact and do a Replace pages but unfortunately, none of these methods did a satisfactory job to begin with. Either they reduced the size so much that the quality was unacceptably pixellated, or they didn't reduce the size at all and in one case actually increased it despite quality loss.

pdftk compress:

no change in size
bookmarks TOC are gone

gs screen:

takes a ridiculously long time and 100% CPU
errors:
    sfopen: gs_parse_file_name failed.                                 ? 
    | ./base/gsicc_manage.c:1651: gsicc_set_device_profile(): cannot find device profile
74.8MB-->10.2MB hideously pixellated
bookmarks TOC are gone

gs printer:

takes a ridiculously long time and 100% CPU
no errors
74.8MB-->66.1MB
light blue background on pages 1-4
bookmarks TOC are gone

gs ebook:

errors:
    sfopen: gs_parse_file_name failed.
      ./base/gsicc_manage.c:1050: gsicc_open_search(): Could not find default_rgb.ic 
    | ./base/gsicc_manage.c:1651: gsicc_set_device_profile(): cannot find device profile
74.8MB-->32.2MB
badly pixellated
bookmarks TOC are gone

qpdf --linearize:

very fast, a few seconds
no size change
bookmarks TOC are gone

pdf2ps:

took very long time
output_pdf2ps.ps 74.8MB-->331.6MB

ps2pdf:

pretty fast
74.8MB-->79MB
very slightly degraded with sl. bluish background
bookmarks TOC are gone
孤星 2024-10-28 12:52:26

这个程序效果很好,

pdf2ps large.pdf very_large.ps

ps2pdf very_large.ps small.pdf

尝试一下。

this procedure works pretty well

pdf2ps large.pdf very_large.ps

ps2pdf very_large.ps small.pdf

give it a try.

本宫微胖 2024-10-28 12:52:26

如果文件大小仍然太大,则可以帮助使用 ps2pdf 降低生成的 pdf 文件的分辨率:

pdf2ps input.pdf tmp.ps
ps2pdf -dPDFSETTINGS=/screen -dDownsampleColorImages=true -dColorImageResolution=200 -dColorImageDownsampleType=/Bicubic tmp.ps output.pdf

调整 -dColorImageResolution 选项的值以获得适合您需要的结果(该值描述了图像分辨率(以 DPI 为单位)。如果您的输入文件是灰度的,则将 Color 替换为 Gray 或使用上述命令中的两个选项也可能有所帮助。通过将 -dPDFSETTINGS 选项更改为 /default/printer 可以进一步微调。有关所有可能选项的说明,请参阅 ps2pdf 手册

If file size is still too large it could help using ps2pdf to downscale the resolution of the produced pdf file:

pdf2ps input.pdf tmp.ps
ps2pdf -dPDFSETTINGS=/screen -dDownsampleColorImages=true -dColorImageResolution=200 -dColorImageDownsampleType=/Bicubic tmp.ps output.pdf

Adjust the value of the -dColorImageResolution option to achieve a result that fits your needs (the value describes the image resolution in DPIs). If your input file is in grayscale, replacing Color through Gray or using both options in the above command could also help. Further fine-tuning is possible by changing the -dPDFSETTINGS option to /default or /printer. For explanations of the all possible options consult the ps2pdf manual.

囍孤女 2024-10-28 12:52:26

一行 pdf2ps 选项(由 Lee)实际上增加了 pdf 大小。不过,两步走一步效果更好。并且可以使用来自 & 的重定向将其合并为一个。到标准输入/输出和管道:

pdf2ps large.pdf - | ps2pdf -small.pdf

确实将 xsane 生成的 PDF 从 18 Mo 减少到 630 ko!

链接丢失,但对于本示例而言,这不是问题......并且是实现所需结果的最简单方法。

The one-line pdf2ps option (by Lee) actually increased the pdf size. However, the two steps one did better. And it can be combined in a single one using redirection from & to standard input/output and pipes:

pdf2ps large.pdf - | ps2pdf - small.pdf

did reduce a PDF generated by xsane from 18 Mo to 630 ko!

Links are lost, but for the present example, it's not a concern... and was the easiest way to achieve the desired result.

pdf2pslarge.pdfsmall.pdf 就足够了,而不需要两步

pdf2ps large.pdf very_large.ps 
ps2pdf very_large.ps small.pdf

,但是 ps2pdflarge.pdfsmall.pdf 是更好的选择。

  • ps2pdf 在没有指定额外参数的情况下要快得多
  • pdf2ps 有时会生成更大的文件。

pdf2ps large.pdf small.pdf is enough, instead of two steps

pdf2ps large.pdf very_large.ps 
ps2pdf very_large.ps small.pdf

However, ps2pdf large.pdf small.pdf is a better choice.

  • ps2pdf is much faster
  • without additional parameters specified, pdf2ps sometimes produces larger file.
白龙吟 2024-10-28 12:52:26

我知道这篇文章已经有很多回复,但我对 使用 PDKTK 创建的 PDF 遇到了同样的问题,我想减小大小

正如我在评论中所说, gs 命令不适合我的情况。

正如评论中已经说过的那样,转换命令的结果对于某些人来说太降级了。

但实际上没有。 “转换”命令可以提供尺寸相当小的正确 PDF。

使用此命令,我的 PDF 上的视觉效果是正确的,压缩率为 74%:

convert -desired 125 original_file.pdf -quality 100 -compress Zip compress_file.pdf

使用此命令,视觉效果是稍微不太正确,但我的 PDF 的压缩率为 81%:

convert -diesel 100 original_file.pdf -quality 100 -compress Zip compress_file.pdf

I know there are already many replies to this post but I had the same problem with a PDF created with PDKTK which I wanted to reduce in size.

And as I had said in the comments, the gs command was not suitable for my case.

And as it had already been said in the comments the result of the convert command was too degraded for some people.

But in reality no. The "convert" command can give a correct PDF with a fairly small size.

With this command, the visual is correct with a compression ratio of 74% on my PDF :

convert -density 125 original_file.pdf -quality 100 -compress Zip compress_file.pdf

With this command, the visual is a little less correct but with a compression ratio of 81% on my PDF :

convert -density 100 original_file.pdf -quality 100 -compress Zip compress_file.pdf

温折酒 2024-10-28 12:52:26

按照 nullglob 建议尝试 gpdf 后,我发现得到了相同的压缩结果(约 900mb 文件降至约 30mb)只需使用cups-pdf 打印机即可。如果您已经在查看文档并且只需要压缩一两个文档,这可能会更容易/首选。

在 Ubuntu 12.04 中,您可以通过

sudo apt-get install cups-pdf

安装后,请务必检查系统工具> > 管理 > 打印 >右键单击“PDF”并将其设置为“启用”

默认情况下,输出将保存到主目录中名为 PDF 的文件夹中。

After trying gpdf as nullglob suggested, I found that I got the same compression results (a ~900mb file down to ~30mb) by just using the cups-pdf printer. This might be easier/preferred if you are already viewing a document and only need to compress one or two documents.

In Ubuntu 12.04, you can install this by

sudo apt-get install cups-pdf

After installation, be sure to check in System Tools > Administration > Printing > right-click 'PDF' and set it to 'enable'

By default, the output is saved into a folder named PDF in your home directory.

命硬 2024-10-28 12:52:26

在尝试了这里列出的所有答案之后,我对包含大量图形的 pdf 获得的最佳结果是

pdftocairo input.pdf output.pdf -pdf

我通过在 Gnome 中使用 Evince 打开 pdf 然后打印到文件发现了这一点。与我的 pdf 文件的所有其他答案相比,这带来了更好的文件压缩和更好的文件质量。当以这种方式打印到文件时,似乎在后台使用 cairo 图形:在结果文件上运行 pdfinfo 显示

制作人:cairo 1.16.0 (https://cairgraphics.org)

After trying all the answers listed here, the best results I have obtained for a pdf with lots of graphics is

pdftocairo input.pdf output.pdf -pdf

I discovered this by opening a pdf with Evince in Gnome and then printing to file. This resulted in better file compression and better file quality compared to all the other answers for my pdf file. It seems cairo graphics is used in the background when printing to a file this way: running pdfinfo on the resulting file reveals

Producer: cairo 1.16.0 (https://cairographics.org)

顾北清歌寒 2024-10-28 12:52:26

Okular 的打印到 PDF

我刚刚使用 Okular 的打印到 PDF 将 Keynote 生成的 140MB PDF 转换为 2.8Mb。文本被转换为光栅,放大太多,清晰地显示像素,但图像保持相当清晰,可用于消息传递应用程序。

Okular's Print to PDF

I just turned a 140MB PDF produced with Keynote into 2.8Mb using Okular's Print to PDF. Text was converted to raster and zooming-in too much cleary shows pixels, but images were kept pretty sharp and its useable for messaging apps.

泪意 2024-10-28 12:52:26

获得了最好的结果

convert -density 125 input.pdf -quality 90 -compress Zip tmp.pdf 

我通过申请

ps2pdf -dPDFSETTINGS=/screen -dDownsampleColorImages=true -dColorImageResolution=200 -dColorImageDownsampleType=/Bicubic tmp.pdf output.pdf

I got the best results by applying

convert -density 125 input.pdf -quality 90 -compress Zip tmp.pdf 

and

ps2pdf -dPDFSETTINGS=/screen -dDownsampleColorImages=true -dColorImageResolution=200 -dColorImageDownsampleType=/Bicubic tmp.pdf output.pdf
赠佳期 2024-10-28 12:52:26

我没有看到使用 qpdf 文件大小有很大减少。我发现最好的方法是在 pdftk 完成后使用 Ghostscript 将 pdf 转换为 postscript,然后再转换回 pdf。在 PHP 中,您将使用 exec:

$ps = $save_path.'/psfile.ps';
exec('ps2ps2 ' . $pdf . ' ' . $ps);
unlink($pdf);
exec('ps2pdf ' .$ps . ' ' . $pdf);
unlink($ps);

几分钟前我使用它将 pdftk 输出从 490k 转换为 71k。

I didn't see a lot of reduction in file size using qpdf. The best way I found is after pdftk is done use ghostscript to convert pdf to postscript then back to pdf. In PHP you would use exec:

$ps = $save_path.'/psfile.ps';
exec('ps2ps2 ' . $pdf . ' ' . $ps);
unlink($pdf);
exec('ps2pdf ' .$ps . ' ' . $pdf);
unlink($ps);

I used this a few minutes ago to take pdftk output from 490k to 71k.

月亮坠入山谷 2024-10-28 12:52:26

我遇到了同样的问题,我使用此函数来压缩各个页面,这导致文件大小被压缩最多为原始大小的 1/3。

for (int i = 1; i <= theDoc.PageCount; i++)
{
       theDoc.PageNumber = i;
       theDoc.Flatten();
}

I had the same issue and I used this function to compress individual pages which results in the file size being compressed by upto 1/3 of the original size.

for (int i = 1; i <= theDoc.PageCount; i++)
{
       theDoc.PageNumber = i;
       theDoc.Flatten();
}
独享拥抱 2024-10-28 12:52:26

In case you want to compress a PDF which contains a lot of selectable text, on Windows you can use NicePDF Compressor - choose "Flate" option.
After trying everything (cpdf, pdftk, gs) it finally helped me to compress my 1360 pages PDF from 500 MB down to 10 MB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文