[pdf] Word: PDF to .docx (headings, paragraphs, tables, images, OCR text for scans); worker ops compress, to_word, page_png
This commit is contained in:
+111
-106
@@ -1,106 +1,111 @@
|
||||
# Formats and the worker protocol
|
||||
|
||||
What Pictor's core reads and writes, which library does each PDF operation, and how the app talks to
|
||||
`pictor-worker`. Version 1 (Day 1, core v0.1.0). The `.pictor` document format arrives on Day 3 and gets its own
|
||||
section here.
|
||||
|
||||
## Images (crates/raster, crates/meta)
|
||||
|
||||
| Format | Read | Write | Metadata strip |
|
||||
| :-- | :-- | :-- | :-- |
|
||||
| PNG | yes | yes | container-level (eXIf, tEXt, zTXt, iTXt, tIME dropped); pixels identical |
|
||||
| JPEG | yes, EXIF orientation applied | yes (quality 88 default; alpha onto white) | container-level (APP1 Exif/XMP, APP13 IPTC, COM, other APPn dropped; JFIF, ICC, Adobe kept); orientation ≠ 1 kept as a 36-byte EXIF; pixels identical |
|
||||
| WebP | yes | lossless only (the image crate's encoder) | EXIF and XMP chunks dropped, VP8X flags fixed |
|
||||
| GIF | yes (first frame for conversion) | single frame | block-level (comments, XMP dropped; every frame and the loop kept) |
|
||||
| TIFF | yes | yes | re-encoded losslessly (uncompressed; ICC and orientation are lost: a known gap); multi-page refused |
|
||||
| BMP | yes | yes | (no metadata) |
|
||||
| ICO | yes | 256/48/32/16 px | — |
|
||||
| HEIC/HEIF | Windows only, through WIC with the codecs Windows has; never bundled | no | convert to JPG first |
|
||||
| AVIF | recognised; decoding needs the AV1 extension (reported as missing) | no | — |
|
||||
|
||||
Formats are recognised by their content (magic bytes), never by the extension. Caps (`pictor_raster::Limits`):
|
||||
128 million pixels, 512 MiB input, 1 GiB decoded; the pixel count is checked from the header before decoding.
|
||||
|
||||
HEIC needs two Store extensions: "HEIF Image Extensions" (free, `9PMMSR1CGPWG`) and an HEVC decoder ("HEVC Video
|
||||
Extensions", `9NMZLZ57R3T7`, US$0.99; checked 2026-11-16). `heif_support()` says which one is missing.
|
||||
|
||||
## PDF (crates/pdf)
|
||||
|
||||
Coordinates between the app and the core are in **view space**: points (1/72 inch) from the top-left corner of the
|
||||
page as displayed (the crop box, `/Rotate` applied). `geometry::PageFrame` converts to PDF user space.
|
||||
|
||||
| Operation | Library | Why |
|
||||
| :-- | :-- | :-- |
|
||||
| open, page sizes, render, text, search, outline | pdfium | what Chrome and Edge use: a file Edge opens, Pictor opens |
|
||||
| reorder, rotate, delete, insert blank, extract, split | lopdf, in place | keeps the form, bookmarks and metadata; deleted pages and their annotations and fields really leave the file (pdfium's save keeps unreferenced objects) |
|
||||
| merge | pdfium page import | handles inherited resources across files; merged forms keep their look but are no longer fillable; bookmarks are not carried over |
|
||||
| images → PDF | lopdf | a JPEG is embedded byte for byte (no re-encoding); others are stored losslessly with an alpha mask |
|
||||
| PDF → PNG/JPG | pdfium | rendering |
|
||||
| form fill | lopdf (`/V`, `/AS` with each checkbox's own on-state) + `NeedAppearances`, then pdfium builds the appearances while rendering with the form | the appearance comes from the same engine as Edge's, Czech letters included; viewers that ignore the flag show the value too |
|
||||
| annotations: highlight, underline, strike, note, free text, ink, rectangle, ellipse, line/arrow | lopdf, each with its own appearance stream | looks the same in every viewer; free text uses Helvetica with WinAnsi plus the Central European letters through `/Differences` |
|
||||
| visual signature (stamp) | lopdf: the image goes into the page content, the old content wrapped in `q … Q` | it is part of the page, not a movable annotation |
|
||||
| flatten | pdfium `FPDFPage_Flatten`, then lopdf drops the form and orphans | |
|
||||
| password on | lopdf, AES-256 (V5/R6), no permission restrictions | pdfium cannot write encryption; qpdf was not needed |
|
||||
| password off | lopdf decrypting load + plain save | |
|
||||
| true redaction | pdfium render + lopdf rebuild (below) | |
|
||||
|
||||
### True redaction
|
||||
|
||||
Each redacted page is rendered by pdfium at 200 dpi (annotations and form fields included), the boxes are painted
|
||||
black into the pixels, and the page is rebuilt as that one JPEG image (same object number, displayed size kept,
|
||||
`/Rotate 0`). The page's old annotations go, fields that lived only there go, resources inherited from the page
|
||||
tree are pushed down to the pages that use them, and the document loses `/Info`, every `/Metadata` and
|
||||
`/PieceInfo`, the struct tree (its ActualText can hold text), bookmarks, attachments, document JavaScript and
|
||||
non-destination open actions. Hidden layers (`/OCProperties`) go when no remaining page uses them; otherwise they
|
||||
stay and the result carries a warning naming those pages. Then every unreferenced object is pruned and the file is
|
||||
written fresh (no incremental history). The other pages' objects are written back byte for byte.
|
||||
|
||||
Cost: the text of a redacted page is no longer selectable or searchable. A content-stream rewrite (cutting out only
|
||||
what is under the box) was not used: it needs a full content-stream interpreter (fonts and glyph positions, form
|
||||
XObjects, inline images, clipping, shadings, Type 3 fonts), and any gap leaves text behind. The tests
|
||||
(`crates/pdf/tests/redaction.rs`) check pdfium text extraction, a lopdf scan of every string, name and stream (raw
|
||||
and decompressed), and a raw byte grep.
|
||||
|
||||
## The worker protocol (crates/worker), version 1
|
||||
|
||||
`pictor-worker.exe` reads one JSON object per line on stdin and answers one per line on stdout. Its first line is
|
||||
`{"hello": "pictor-worker", "protocol": 1, "version": "0.1.0"}`; the client refuses another protocol.
|
||||
|
||||
```
|
||||
→ {"id": 7, "op": "pdf.render", "args": {"doc": 1, "page": 0, "width": 1200}}
|
||||
← {"id": 7, "ok": {"png": "<base64>", "width": 1200, "height": 1553}}
|
||||
← {"id": 7, "err": {"kind": "needs_password", "message": "This PDF is password-protected. Enter its password to open it."}}
|
||||
```
|
||||
|
||||
Error kinds: `needs_password`, `wrong_password`, `damaged`, `unsupported`, `too_large`, `codec_missing`, `invalid`,
|
||||
`io`, `exists`, `not_open`, `cannot_reach_target`, `engine_missing`; the client adds `timed_out`,
|
||||
`out_of_memory`, `crashed` and `spawn`. `message` is a plain sentence for the user.
|
||||
|
||||
| Operation | Arguments | Result |
|
||||
| :-- | :-- | :-- |
|
||||
| `pdf.open` | `path`, `password?` | `doc` (a session id), `info` (pages with sizes and labels, outline, `has_form`) |
|
||||
| `pdf.close` | `doc` | — |
|
||||
| `pdf.render` | `doc`, `page`, `width` (px) | `png` (base64), `width`, `height` |
|
||||
| `pdf.text` | `doc` | `pages`: text per page |
|
||||
| `pdf.search` | `doc`, `query` | `hits`: `{page, rects}` in view space (max 2000) |
|
||||
| `pdf.form` | `doc` | `fields`: name, kind, value, options, page, rect, read_only, multiline |
|
||||
| `pdf.save` | `doc`, `edits` (see `pictor_pdf::Edits`), `output`, `mode` (`new` / `overwrite`) | `path`, `bytes`, `bak?`, `warnings` |
|
||||
| `pdf.extract` | `doc`, `pages`, `output` | `path` |
|
||||
| `pdf.to_images` | `doc`, `pages`, `dpi`, `format`, `output` (with `{n}`) | `paths` |
|
||||
| `pdf.merge` | `inputs` [{`path`, `password?`}], `output` | `path` |
|
||||
| `pdf.split` | `path`, `password?`, `ranges` ("1-3, 5, 8-"), `output` (with `{n}`) | `paths` |
|
||||
| `pdf.from_images` | `inputs`, `output`, `fit` (`image`/`a4`/`letter`), `margin` | `path` |
|
||||
| `image.probe` | `path` | `format`, `width`, `height`, `has_alpha`, `frames`, `bytes` |
|
||||
| `image.thumb` | `path`, `max` | `png`, original `width`, `height` |
|
||||
| `image.convert` | `input`, `output`, `format`, `quality?`, `crop?`, `rotate?`, `resize?`, `preset?`, `target_bytes?`, `overwrite?` | `path`, `bytes`, `format`, `width`, `height` |
|
||||
| `image.heif_support` | — | `support` |
|
||||
| `meta.read` | `path` | `report` (items by group), `groups` (with labels) |
|
||||
| `meta.strip` | `input`, `output`, `overwrite?` | `path`, `removed` |
|
||||
| `qr.render` | `payload`, `level`, `style`, `logo?` (a path) | `png`, `svg`, `level_used`, `size`, `text` |
|
||||
|
||||
Saving: `mode: new` refuses to replace a file (the app picks `<name>-edited.pdf`); `overwrite` writes
|
||||
`<file>.bak` first, then replaces the file in one rename (`crates/save`).
|
||||
|
||||
Caps: files over 1 GB are refused; the client runs the worker in a Job Object (2 GB by default, killed with the
|
||||
app) and gives each request a time cap; a worker that hangs or dies is replaced on the next request (open documents
|
||||
must then be opened again).
|
||||
# Formats and the worker protocol
|
||||
|
||||
What Pictor's core reads and writes, which library does each PDF operation, and how the app talks to
|
||||
`pictor-worker`. Version 1 (Day 1, core v0.1.0). The `.pictor` document format arrives on Day 3 and gets its own
|
||||
section here.
|
||||
|
||||
## Images (crates/raster, crates/meta)
|
||||
|
||||
| Format | Read | Write | Metadata strip |
|
||||
| :-- | :-- | :-- | :-- |
|
||||
| PNG | yes | yes | container-level (eXIf, tEXt, zTXt, iTXt, tIME dropped); pixels identical |
|
||||
| JPEG | yes, EXIF orientation applied | yes (quality 88 default; alpha onto white) | container-level (APP1 Exif/XMP, APP13 IPTC, COM, other APPn dropped; JFIF, ICC, Adobe kept); orientation ≠ 1 kept as a 36-byte EXIF; pixels identical |
|
||||
| WebP | yes | lossless only (the image crate's encoder) | EXIF and XMP chunks dropped, VP8X flags fixed |
|
||||
| GIF | yes (first frame for conversion) | single frame | block-level (comments, XMP dropped; every frame and the loop kept) |
|
||||
| TIFF | yes | yes | re-encoded losslessly (uncompressed; ICC and orientation are lost: a known gap); multi-page refused |
|
||||
| BMP | yes | yes | (no metadata) |
|
||||
| ICO | yes | 256/48/32/16 px | — |
|
||||
| HEIC/HEIF | Windows only, through WIC with the codecs Windows has; never bundled | no | convert to JPG first |
|
||||
| AVIF | recognised; decoding needs the AV1 extension (reported as missing) | no | — |
|
||||
|
||||
Formats are recognised by their content (magic bytes), never by the extension. Caps (`pictor_raster::Limits`):
|
||||
128 million pixels, 512 MiB input, 1 GiB decoded; the pixel count is checked from the header before decoding.
|
||||
|
||||
HEIC needs two Store extensions: "HEIF Image Extensions" (free, `9PMMSR1CGPWG`) and an HEVC decoder ("HEVC Video
|
||||
Extensions", `9NMZLZ57R3T7`, US$0.99; checked 2026-11-16). `heif_support()` says which one is missing.
|
||||
|
||||
## PDF (crates/pdf)
|
||||
|
||||
Coordinates between the app and the core are in **view space**: points (1/72 inch) from the top-left corner of the
|
||||
page as displayed (the crop box, `/Rotate` applied). `geometry::PageFrame` converts to PDF user space.
|
||||
|
||||
| Operation | Library | Why |
|
||||
| :-- | :-- | :-- |
|
||||
| open, page sizes, render, text, search, outline | pdfium | what Chrome and Edge use: a file Edge opens, Pictor opens |
|
||||
| reorder, rotate, delete, insert blank, extract, split | lopdf, in place | keeps the form, bookmarks and metadata; deleted pages and their annotations and fields really leave the file (pdfium's save keeps unreferenced objects) |
|
||||
| merge | pdfium page import | handles inherited resources across files; merged forms keep their look but are no longer fillable; bookmarks are not carried over |
|
||||
| images → PDF | lopdf | a JPEG is embedded byte for byte (no re-encoding); others are stored losslessly with an alpha mask |
|
||||
| PDF → PNG/JPG | pdfium | rendering |
|
||||
| form fill | lopdf (`/V`, `/AS` with each checkbox's own on-state) + `NeedAppearances`, then pdfium builds the appearances while rendering with the form | the appearance comes from the same engine as Edge's, Czech letters included; viewers that ignore the flag show the value too |
|
||||
| annotations: highlight, underline, strike, note, free text, ink, rectangle, ellipse, line/arrow | lopdf, each with its own appearance stream | looks the same in every viewer; free text uses Helvetica with WinAnsi plus the Central European letters through `/Differences` |
|
||||
| visual signature (stamp) | lopdf: the image goes into the page content, the old content wrapped in `q … Q` | it is part of the page, not a movable annotation |
|
||||
| flatten | pdfium `FPDFPage_Flatten`, then lopdf drops the form and orphans | |
|
||||
| password on | lopdf, AES-256 (V5/R6), no permission restrictions | pdfium cannot write encryption; qpdf was not needed |
|
||||
| password off | lopdf decrypting load + plain save | |
|
||||
| true redaction | pdfium render + lopdf rebuild (below) | |
|
||||
| compress presets (Day 2) | lopdf: images capped at the largest page side × the preset's dpi and re-encoded as JPEG (E-mail 120 → 96 → 72 dpi until ≤ 5 MB, Web 150, Print 300); Flate everywhere; object streams | text, fonts and vectors untouched, so text stays extractable; fonts are not subset (gap) |
|
||||
| PDF to Word (Day 2) | pdfium characters (box, size, weight, slant) → lines, segments, tables, paragraphs, headings; images at their place; docx-rs writes the .docx | "Layout is approximate"; scanned pages are reported, and OCR text from Fundamentum's Lector can be passed in |
|
||||
|
||||
### True redaction
|
||||
|
||||
Each redacted page is rendered by pdfium at 200 dpi (annotations and form fields included), the boxes are painted
|
||||
black into the pixels, and the page is rebuilt as that one JPEG image (same object number, displayed size kept,
|
||||
`/Rotate 0`). The page's old annotations go, fields that lived only there go, resources inherited from the page
|
||||
tree are pushed down to the pages that use them, and the document loses `/Info`, every `/Metadata` and
|
||||
`/PieceInfo`, the struct tree (its ActualText can hold text), bookmarks, attachments, document JavaScript and
|
||||
non-destination open actions. Hidden layers (`/OCProperties`) go when no remaining page uses them; otherwise they
|
||||
stay and the result carries a warning naming those pages. Then every unreferenced object is pruned and the file is
|
||||
written fresh (no incremental history). The other pages' objects are written back byte for byte.
|
||||
|
||||
Cost: the text of a redacted page is no longer selectable or searchable. A content-stream rewrite (cutting out only
|
||||
what is under the box) was not used: it needs a full content-stream interpreter (fonts and glyph positions, form
|
||||
XObjects, inline images, clipping, shadings, Type 3 fonts), and any gap leaves text behind. The tests
|
||||
(`crates/pdf/tests/redaction.rs`) check pdfium text extraction, a lopdf scan of every string, name and stream (raw
|
||||
and decompressed), and a raw byte grep.
|
||||
|
||||
## The worker protocol (crates/worker), version 1
|
||||
|
||||
`pictor-worker.exe` reads one JSON object per line on stdin and answers one per line on stdout. Its first line is
|
||||
`{"hello": "pictor-worker", "protocol": 1, "version": "0.1.0"}`; the client refuses another protocol.
|
||||
|
||||
```
|
||||
→ {"id": 7, "op": "pdf.render", "args": {"doc": 1, "page": 0, "width": 1200}}
|
||||
← {"id": 7, "ok": {"png": "<base64>", "width": 1200, "height": 1553}}
|
||||
← {"id": 7, "err": {"kind": "needs_password", "message": "This PDF is password-protected. Enter its password to open it."}}
|
||||
```
|
||||
|
||||
Error kinds: `needs_password`, `wrong_password`, `damaged`, `unsupported`, `too_large`, `codec_missing`, `invalid`,
|
||||
`io`, `exists`, `not_open`, `cannot_reach_target`, `engine_missing`; the client adds `timed_out`,
|
||||
`out_of_memory`, `crashed` and `spawn`. `message` is a plain sentence for the user.
|
||||
|
||||
| Operation | Arguments | Result |
|
||||
| :-- | :-- | :-- |
|
||||
| `pdf.open` | `path`, `password?` | `doc` (a session id), `info` (pages with sizes and labels, outline, `has_form`) |
|
||||
| `pdf.close` | `doc` | — |
|
||||
| `pdf.render` | `doc`, `page`, `width` (px) | `png` (base64), `width`, `height` |
|
||||
| `pdf.text` | `doc` | `pages`: text per page |
|
||||
| `pdf.search` | `doc`, `query` | `hits`: `{page, rects}` in view space (max 2000) |
|
||||
| `pdf.form` | `doc` | `fields`: name, kind, value, options, page, rect, read_only, multiline |
|
||||
| `pdf.save` | `doc`, `edits` (see `pictor_pdf::Edits`), `output`, `mode` (`new` / `overwrite`) | `path`, `bytes`, `bak?`, `warnings` |
|
||||
| `pdf.extract` | `doc`, `pages`, `output` | `path` |
|
||||
| `pdf.compress` | `doc`, `preset` (`email` ≤ 5 MB / `web` / `print`), `output` | `path`, `before`, `after`, `images_changed`, `warnings` |
|
||||
| `pdf.to_word` | `doc`, `output` (.docx), `ocr?` ({page: text} for scanned pages) | `path`, `pages`, `scanned_pages` |
|
||||
| `pdf.page_png` | `doc`, `page`, `dpi`, `output` | `path` (a page for OCR) |
|
||||
| `pdf.to_images` | `doc`, `pages`, `dpi`, `format`, `output` (with `{n}`) | `paths` |
|
||||
| `pdf.merge` | `inputs` [{`path`, `password?`}], `output` | `path` |
|
||||
| `pdf.split` | `path`, `password?`, `ranges` ("1-3, 5, 8-"), `output` (with `{n}`) | `paths` |
|
||||
| `pdf.from_images` | `inputs`, `output`, `fit` (`image`/`a4`/`letter`), `margin` | `path` |
|
||||
| `image.probe` | `path` | `format`, `width`, `height`, `has_alpha`, `frames`, `bytes` |
|
||||
| `image.thumb` | `path`, `max` | `png`, original `width`, `height` |
|
||||
| `image.convert` | `input`, `output`, `format`, `quality?`, `crop?`, `rotate?`, `resize?`, `preset?`, `target_bytes?`, `overwrite?` | `path`, `bytes`, `format`, `width`, `height` |
|
||||
| `image.heif_support` | — | `support` |
|
||||
| `meta.read` | `path` | `report` (items by group), `groups` (with labels) |
|
||||
| `meta.strip` | `input`, `output`, `overwrite?` | `path`, `removed` |
|
||||
| `qr.render` | `payload`, `level`, `style`, `logo?` (a path) | `png`, `svg`, `level_used`, `size`, `text` |
|
||||
|
||||
Saving: `mode: new` refuses to replace a file (the app picks `<name>-edited.pdf`); `overwrite` writes
|
||||
`<file>.bak` first, then replaces the file in one rename (`crates/save`).
|
||||
|
||||
Caps: files over 1 GB are refused; the client runs the worker in a Job Object (2 GB by default, killed with the
|
||||
app) and gives each request a time cap; a worker that hangs or dies is replaced on the next request (open documents
|
||||
must then be opened again).
|
||||
|
||||
Reference in New Issue
Block a user