跳到正文
DEV Community· Anuj Kabra·· 4 小時前AI 評分56

分享 PDF 前如何只保留對方要求的頁面

Send only the pages they asked for: splitting a PDF before you share it

AI 導讀

作者測試多種 PDF 分頁方式,示範如何只分享指定頁面,並指出部分抽取方式會保留已刪頁的文件層級資料。測試中,qpdf 使用 `--empty` 產生的檔案沒有標題、作者、主旨或書籤;可用 `pdfinfo` 和閲讀器的書籤窗格檢查,而保留頁面上印出的敏感資料不會因此消失。

正文

When someone asks for "page 3 of your statement" or "the discharge summary", the easy thing is to send the whole PDF. That hands over every other page too: other transactions, other diagnoses, an address, an account number. Cutting the file down to the pages they asked for takes a minute, and there's one detail that catches people out: some ways of doing it keep data from the pages you removed. I tested everything below on a dummy 8-page bank statement I generated with fake numbers (title, author and an account number in its metadata, one bookmark per page).

The built-in routes

You probably already have something that can do this.

  • macOS Preview: View > Thumbnails, select the pages you want (Cmd-click for several), and drag them onto the desktop. Preview makes a new PDF from just those pages. Another way is File > Print, set the page range, then PDF > Save as PDF at the bottom left.
  • Chrome or Edge, any OS: open the PDF in the browser, Print, choose "Save as PDF" (Chrome) or "Microsoft Print to PDF" (Edge on Windows) as the printer, set Pages to Custom and type something like 3-4 or 2,5.
  • Acrobat Reader: the free Reader can't extract pages itself, but its Print dialog with "Microsoft Print to PDF" or the Mac "Save as PDF" does the same job.

I ran the tests on Linux, so I haven't clicked through these apps for this post. The print routes write the pages out again through the printing system rather than copying them, so check the result: zoom in on small print, and see if the text can still be selected if that matters to whoever's receiving it.

The command line: qpdf

qpdf copies pages without re-rendering them, so text, fonts and images stay exactly as they were. I used qpdf 12.4.2.

qpdf --empty --pages statement.pdf 3-4 -- pages-3-4.pdf

--empty starts from a blank document, --pages names the input and the range, and -- ends the page list. The range syntax is worth knowing:

3-4        pages 3 and 4
2,5        page 2 then page 5, in the order you type them
7-z        page 7 to the last page (z is the last page)
r2-r1      the last two pages, counted from the end
1-z,x5     every page except 5

I checked each one by pulling the text back out of the result with pdftotext and looking for a marker I'd printed on every page. 1-z,x5 and 1-4,6-8 both gave the same 7 pages, and 5,2 really did put page 5 first.

The gotcha: what comes along from the original

qpdf's own help (qpdf --help=page-selection) starts with a form that uses an input file as the base instead of --empty, and says "Document-level information, such as outlines, tags, etc., is taken from in.pdf and is preserved in out.pdf." Here's what that means in practice:

qpdf statement.pdf --pages . 3-4 -- pages-3-4.pdf

The . means "the main input file". This gives the same two pages, but it keeps what belongs to the document rather than to a page. Here's what each output carried, from pdfinfo and a count of bookmark entries with pypdf:

                         pages  Title / Author / Subject    bookmarks
original                   8    yes (incl. account number)      8
qpdf ... --pages . 3-4     2    yes, all kept                   8
qpdf --empty --pages 3-4   2    none                            0
pdfseparate + pdfunite     2    none                            0

So the two-page file from the . form still had the account number in its Subject field and all eight bookmarks, which name pages that aren't in it anymore. In a real statement, bookmark titles and metadata can say a lot. Use --empty when the point is sharing less, and check with:

pdfinfo pages-3-4.pdf

and look at the bookmarks pane in your viewer. pdfinfo only shows the info dictionary; there can also be XMP metadata, which exiftool -a -G1 file.pdf lists.

One more thing to check before sending: a split never changes what's printed on the pages you keep. If page 3 has your full account number in its header, so does the extract.

Poppler, if that's what you have

pdfseparate writes one file per page, and pdfunite joins them:

pdfseparate -f 3 -l 4 statement.pdf page-%d.pdf
pdfunite page-3.pdf page-4.pdf pages-3-4.pdf

%d becomes the page number. Neither tool brought over the metadata or bookmarks in my test.

Password-protected files

Most bank statements open with a password, and splitting needs that password because the page tree is encrypted. qpdf without it:

$ qpdf --empty --pages locked.pdf 3-4 -- out.pdf
qpdf: locked.pdf: invalid password

With it, you put --password= after the file name inside the page list:

qpdf --empty --pages locked.pdf --password=YOURPASSWORD 3-4 -- out.pdf

The result came out unencrypted (pdfinfo says Encrypted: no), which is usually what the person asking expects, but it means the extract no longer needs a password, so send it the way you'd send any unprotected document. pdfseparate failed on the locked file with "Incorrect password", and the version I have (poppler 25.03.0) has no password option at all, so remove the password first if you're using poppler. Typing a password on the command line also leaves it in your shell history. qpdf has --password-file=, but it isn't accepted inside the --pages list (I got "unrecognized argument"), so to keep the password out of history, decrypt first and split the decrypted copy:

qpdf --decrypt --password-file=pw.txt locked.pdf unlocked.pdf
qpdf --empty --pages unlocked.pdf 3-4 -- pages-3-4.pdf

Then delete pw.txt and unlocked.pdf.

A browser option

I built ToolOcean, and its split page does the --empty kind of extraction in the browser without uploading the file. I ran the dummy statement through the live page in headless Chrome with the range 3-4: it saved split_pages_3-4.pdf with 2 pages, no Title, Author or Subject, and no bookmarks. The only network request after I picked the file was a 326-byte analytics ping, nowhere near the size of the PDF. Two differences from qpdf: it sorts pages into ascending order, so 5,2 gives page 2 first, and it can't open a password-protected file (it showed "Could not read the PDF file" for my locked copy), so remove the password first: toolocean.co/pdf-split.

Drafted with AI help, tested and checked by me.

來源:DEV Community · dev.to