Computer >> Hướng Dẫn Máy Tính >  >> Phần Mềm >> Office

Làm Phẳng PDF Chuyên Nghiệp: Xóa An Toàn Layers Ẩn Trong Một Bước

Vài ngày trước, tôi show redact information trong Okular, default PDF viewer Plasma desktop. Action đơn giản, nhưng không effectively destroy redacted info, chỉ obscure từ viewer.

Hôm nay tôi show phần 2 của puzzle - flattening PDF documents. Hãy tưởng tượng ảnh multiple layers, save non-layered format. Info sau đó flattened thành single layer - giá trị pixel vertically stacked được tính - added/subtracted/whatever - presented as single definitive computation. PDF tương tự, nhưng phức tạp hơn, given PDF structure. Làm đi.

Tool of the trade - Ghostscript

Bạn có thể nghe Ghostscript (gs) trước - tôi nói qua các bài LaTeX/LyX. Bây giờ, dùng gs engine process "multi-layered" PDF thành flattened, info redacted properly. Làm Linux, vì Linux làm better specific, focused tasks file format processing. Ghostscript available distro, nếu chưa cài, có trong repo.

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

Command này làm magic. Không additional processing images trong PDF - đừng expect size reduction lớn. Mục đích không phải đó. Nhưng effectively, đó là nó.

Alternative methods - ImageMagick và pdf2ps

Nếu trên không satisfy, 2 approach khác. Lưu ý, tôi thấy less effective hơn gs. Nhớ khi đi ahead với suggestions dưới.

Thử ImageMagick convert utility. But wait.

convert -density 300 original.pdf flattened.pdf

Mặc định, do security vulnerability Ghostscript engine, ImageMagick config không process various files: PS, PDF, EPS, XPS, others. Thử không edit policy, bạn thấy 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.

Để resolve, edit file - replace [NUMBER] với 6 hoặc 7 cho version:

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

File này có list security policies:

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

Đổi PDF rights="none" thành rights="read|write". Nếu lo security, chỉ temporary change khi flatten file cụ thể, revert back 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>

Bây giờ process file. Thường lâu hơn gs trực tiếp. Hơn nữa, file rất lớn, có thể out of memory, conversion fail. Ví dụ:

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

Lại cần change XML config, increase memory limits policies. Tuy nhiên, document có thể quá lớn cho memory resources. Tôi không có magic set 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"/>
...

Tool thứ 2: cặp pdf2ps và ps2pdf. Cơ bản, convert PDF sang PS rồi back to PDF, effectively flatten. Magic command:

pdf2ps original.pdf - | ps2pdf - flattened.pdf

Chạy fine và fast - nhưng, default resulting low-res images do aggressive compression. Cần consider và experiment options tools, retain document fidelity bạn cần.

Kết luận

Đây bạn học flatten PDF. Technical intricacies beyond scope, nhưng bạn có tools done job. Experience tôi: Ghostscript fastest, most effective, best results.

Bạn cũng thử ImageMagick và pdf2ps/ps2pdf combo. Tuy nhiên, tôi less pleased results. Anyway, nếu phải share PDF với người khác, muốn redact snippets info, bạn có 2-tutorial process sorted. Guide 2 hoàn thiện picture. Bye bye.

Chào.