跳到正文
DEV Community· Anuj Kabra·· 2 小時前AI 評分61

如何檢查及清除 PDF 中繼資料,避免舊資料殘留

My PDF was telling people who made it: checking and cleaning PDF metadata

AI 導讀

Anuj Kabra 示範如何檢查及清除 PDF 的 Info dictionary 與 XMP 封包,並指出單用 exiftool 移除標籤後,舊資料仍可能留在檔案中並可復原。

正文

Before you send a PDF outside your team, it's worth knowing that the file carries a few labels you never typed on the page: an author name, a title, the app that made it, and when. Those labels often hold a draft title or your computer's account name. Here's how to see them and remove them with free tools, and why the most common one-liner doesn't fully remove anything.

Where the labels live

A PDF can describe itself in two places:

  1. The Info dictionary. A small key/value table: Title, Author, Subject, Keywords, Creator (the app that made the document), Producer (the library that wrote the PDF), and the creation and modification dates.
  2. The XMP packet. An XML block stored as a metadata stream. It usually repeats the same ideas (for example dc:creator for the author and xmp:CreatorTool for the app) and can hold more.

Cleaning only one of them leaves the other behind, so check both.

Reading them

I made a one-page test PDF with an author, a title and an XMP packet, then read it.

pdfinfo (from poppler-utils, on most Linux distros and in Homebrew) shows the Info dictionary:

$ pdfinfo sample.pdf
Title:           Draft offer letter v3
Subject:         Internal
Keywords:
Author:          Test Author
Creator:         Writer on my laptop
Producer:        ReportLab PDF Library - (opensource)
...
Metadata Stream: yes

That last line tells you an XMP packet exists. pdfinfo -meta sample.pdf prints it as raw XML.

exiftool reads both places and labels which one each value came from:

$ exiftool -a -G1 -s sample.pdf
[PDF]      Author        : Test Author
[PDF]      Creator       : Writer on my laptop
[PDF]      Title         : Draft offer letter v3
[XMP-dc]   Creator       : Test Author
[XMP-xmp]  CreatorTool   : Writer on my laptop

[PDF] is the Info dictionary and [XMP-...] is the XMP packet.

The one-liner that only hides things

The usual advice is:

exiftool -all:all= file.pdf

Afterwards, exiftool and pdfinfo both report no author. But exiftool prints a warning while it works:

Warning: [minor] ExifTool PDF edits are reversible. Deleted tags may be recovered!

It means what it says. For PDFs, exiftool appends an update to the end of the file instead of rewriting it, so the old Info dictionary is still in the bytes. On my test file, grep -c "Test Author" still found the name twice after the "strip", and this command brought every value back:

exiftool -pdf-update:all= file.pdf

So after exiftool, rewrite the file so the unreferenced old objects are dropped. qpdf does that:

exiftool -all:all= file.pdf
qpdf --linearize file.pdf clean.pdf

On clean.pdf, grep found no copy of the name, and exiftool showed no author.

Another route: copy only the pages

If you have Python, pypdf can build a new PDF from the pages alone, which leaves out the old Info dictionary and the XMP packet:

import sys
from pypdf import PdfReader, PdfWriter

r = PdfReader(sys.argv[1])
w = PdfWriter()
for page in r.pages:
    w.add_page(page)
w.write(sys.argv[2])

python strip.py in.pdf out.pdf gave a file where pdfinfo showed only Producer: pypdf and Metadata Stream: no. Bookmarks, form fields and other document-level parts don't come across this way, so check the output.

What none of this touches

Metadata is not page content. My test page said "Prepared by Test Author", and after every method above, pdftotext clean.pdf - still printed that line. A name in the text, in a header or footer, in a comment, or inside an attached file has to be removed in the source document or with a real redaction step. Also, don't use grep to check page text: page streams are usually compressed, so grep can miss text that is plainly visible on the page.

The cheapest fix is often upstream: clear the author and title in the document properties of the app you write in, then export again.

A browser option

I built ToolOcean, and it has a page that reads both the Info dictionary and the XMP packet in your browser, lets you edit or strip them, and saves a new copy without uploading the file. Its strip doesn't touch page content either, and it refuses encrypted files: toolocean.co/pdf-metadata.

Drafted with AI help, tested and checked by me.

來源:DEV Community · dev.to