Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ซอฟต์แวร์ >> Office

Master PDF Flattening: ลบ Hidden Layers อย่างปลอดภัยใน One Step

ไม่กี่วันก่อน ผมโชว์วิธี Redact Information ใน Okular Default PDF Viewer ใน Plasma Desktop Action ค่อนข้าง Simple แต่ไม่ Effectively Destroy Redacted Information เพียงแค่ Obscure จาก Viewer

สิ่งที่อยากโชว์วันนี้คือ Second Part of the Puzzle - Flattening of PDF Documents คิด Image with Multiple Layers, แล้ว Save All in Non-layered Format Information แล้ว Flatten into Single Layer - Values of All Vertically Stacked Pixels Calculated - Added/Subtracted/Whatever - 然後 Presented as Single Definitive Computation of This Action Same with PDF, Except More Complicated, Given PDF Structure Let's Do It

Tool of the Trade - Ghostscript

อาจเคยได้ยิน Ghostscript (gs) ก่อน - ผมพูดถึงมันตลอดปีใน Articles ต่างๆ เกี่ยวกับ LaTeX และ LyX ตอนนี้เราจะใช้ GS Engine Process Existing "Multi-layered" PDF เป็น Flattened One โดย Information จะ Redacted Properly เราจะทำใน Linux เพราะถ้ามี One Thing Linux Does Way Better Than Other OS - Specific, Focused Tasks with File Format Processing Ghostscript Should Be Available in Your Distro, และถ้าไม่ Installed, จะอยู่ใน Repository Archives

gs -sDEVICE=pdfwrite -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -sOutputFile=flattened.pdf input.pdf

Command นี้คือ Magic ไม่ทำ Additional Processing of Images ใน PDF Files - 所以ไม่ควร Expect Great Reduction in Size 毕竟那不是 Purpose 但 Effectively, That's It

Alternative Methods - ImageMagick และ pdf2ps

如果 Above 不 Satisfy You 為什麼理由 有 Two Other Approaches Could Try 然而請注意, 我發現這些 Less Effective Than 使用 GS 所以記住當你 Go Ahead with Suggestions Below

可以試 ImageMagick Convert Utility 但 Wait

convert -density 300 original.pdf flattened.pdf

By Default, 由於 Security Vulnerability in Ghostscript Engine, ImageMagick Configured 不 Process Various Files, 包括 PS, PDF, EPS, XPS, 和 Others 所以如果 Try Without Editing ImageMagick Policies 你會 See Following Error:

convert-im6.q16: attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408.
convert-im6.q16: no images defined `flattened.pdf' @ error/convert.c/ConvertImageCommand/3258.

To Resolve, 需要 Edit File - Replace [NUMBER] with 6 or 7 for Relevant Version:

/etc/ImageMagick-[NUMBER]/policy.xml

在這 File, 有 List of Security Policies:

...
  <policy domain="coder" rights="none" pattern="EPS" />
  <policy domain="coder" rights="none" pattern="PDF" />
  <policy domain="coder" rights="none" pattern="XPS" />
</policymap>

Change PDF one so Rights="none" becomes Rights="read|write" 如果 Worried about Security 可以 Only Make Temporary Change While Flattening Particular File, And Then Revert Back to More Robust Settings

...
  <policy domain="coder" rights="none" pattern="EPS" />
  <policy domain="coder" rights="read|write" pattern="PDF" />
  <policy domain="coder" rights="none" pattern="XPS" />
</policymap>

Now, 可以 Process File Typically, 這將 Take Longer Than 使用 GS Directly 此外, 如果 Working with Very Large Files, 可能也 Run Out of Memory, 和 Conversion Will Fail 例如:

convert-im6.q16: cache resources exhausted `flattened.pdf' @ error/cache.c/OpenPixelCache/4083 `flattened.pdf' @ error/pdf.c/WritePDFImage/2341.

Once Again, 需要 Change XML Config File 和 Increase Memory Limits in Different Policies 然而, 很可能 Document 太大 for Available Memory Resources 你有在 System 我沒有 Magic Set to Suggest:

...
  <policy domain="resource" name="memory" value="2048MiB"/>
  <policy domain="resource" name="map" value="4096MiB"/>
  <policy domain="resource" name="width" value="256KP"/>
  <policy domain="resource" name="height" value="256KP"/>
  <policy domain="resource" name="area" value="1024MB"/>
  <policy domain="resource" name="disk" value="4GiB"/>
...

The Second Tool 是 Pair of pdf2ps 和 ps2pdf 基本上, 你想 Convert PDF to PS File 然後 Back to PDF, Which Will Effectively Flatten Document Magic Command Does It:

pdf2ps original.pdf - | ps2pdf - flattened.pdf

This Works Fine and Fast - 但, Defaults Result in Low-res Images Due to Aggressive Compression 這是 Something to Take into Account 和 Experiment with Different Options in Tools, 確保 Retain Document Fidelity You Require

ข้อสรุป

And There You Go 現在你學會如何 Flatten PDF Files Technical Intricacies of How All This Works Beyond Scope of This Article 但至少你有 Tools to Get the Job Done My Experience Shows Ghostscript to be Fastest 和 Most Effective, While Producing Best Results Along the Way

You Can Also Try Other Two Utilities - ImageMagick 和 pdf2ps/ps2pdf Combo 然而, 我 Less Pleased with Results Anyway, 如果必須 Share PDF Files with Others, 和想 Redact Snippets of Information Contained Therein, 你現在有 Two-Tutorial Process to Get This Sorted This Second Guide Completes the Picture. Bye Bye Now.

Cheers.