fix(docx): keep figures when a DOCX is printed to PDF

Two bugs, both visible in 平台技术方案(带图测试专用).docx: one figure, eleven
tables and 357 paragraphs, whose PDF came out as a text-only document.

First, AppKit's OOXML reader returns the text of a DOCX and silently drops every
inline drawing - an attributed string with zero attachments for a document with a
picture in it. That is not a rendering loss, it is a parse loss, so no margin,
print setting or layout change can recover the figure: by the time the page is
drawn, the picture is simply not in the attributed string. A DOCX whose body
contains an image is now converted by Pandoc into HTML with its media extracted
beside it, and the HTML - which Pandoc reads faithfully, headings, tables and
pictures - goes to the same printer. Image-only DOCX files keep the existing
page-scan path, which runs first, and a machine without Pandoc falls back to the
old text-only behaviour rather than failing.

Second, once pictures could arrive they printed as a cropped fragment of
themselves. The layout sizes an attachment from the density header inside the
file, assumes 72 dpi when there is none, and ignores both bounds and CSS width: a
1600 px screenshot at 96 dpi asks for 1200 points and lands on a 595 pt page.
Measured across PNG pHYs and JPEG JFIF, the placed width is exactly
pixels / dpi * 64. A plain NSTextAttachment does honour bounds, so the renderer
now replaces any oversized attachment with one that fits the text column; the
print operation scales it to the page with the text, landing at 499pt, which is
the printable width the body text already uses. Nothing is resampled, only the
size the layout asks for changes. This also fixes Markdown and HTML documents
with wide images, which had the same overflow.

Both halves are pinned by tests, and both fail against the previous build:
keeps_pictures_when_rendering_a_text_docx_to_pdf sees no image at all, and
fits_a_wide_picture_onto_the_page reports a picture placed 1066.77pt wide on a
595pt page. The second reads the Form XObject's /BBox, so it needs no content
stream decoded and no pixel compared.
main
mula.liu 2026-10-26 14:50:52 +08:00
parent 58949bebd2
commit 19602b79f0
10 changed files with 241 additions and 4 deletions

View File

@ -121,7 +121,7 @@ The format ring for a given file type. Every target below is wired in `src-tauri
| PDF | `RTF` | `lopdf` + Pandoc | Text layer becomes the document body; needs Pandoc |
| PDF | `HEIC` | PDFKit / Poppler + `sips` | Pages render as PNG then transcode; needs a system HEIC codec |
| PDF | `PNG` `JPG` `TIFF` | PDFKit / Poppler | One file per page into a `… pages` folder |
| Document `docx` `rtf` `odt` `txt` `md` `html` | `PDF` | AppKit renderer (macOS) · Pandoc elsewhere | macOS path needs no LaTeX or wkhtmltopdf |
| Document `docx` `rtf` `odt` `txt` `md` `html` | `PDF` | AppKit renderer (macOS) · Pandoc elsewhere | macOS path needs no LaTeX or wkhtmltopdf. A DOCX with pictures goes through Pandoc → HTML first, because the AppKit reader drops images |
| Document `docx` `rtf` `odt` `txt` `md` `html` | `DOCX` `RTF` `HTML` `TXT` `MD` | Pandoc | The source format is omitted; Markdown writes as GFM. HTML is a self-contained file with embedded images |
| Presentation `.pptx` | `MD` `PDF` | Pure Rust + Pandoc | Per-slide headings, bullet text and **tables as real Markdown tables**; PDF is a text summary, not a layout render |
| Spreadsheet | `MD` `PDF` | `calamine` / Rust | One Markdown table per sheet; `csv`/`tsv` parsed directly |

View File

@ -121,7 +121,7 @@ Shift 常驻菜单栏(macOS)与通知区域(Windows),同时也有普
| PDF | `RTF` | `lopdf` + Pandoc | 以文字层作为正文;需要 Pandoc |
| PDF | `HEIC` | PDFKit / Poppler + `sips` | 先渲染为 PNG 再转码;需要系统 HEIC 编码器 |
| PDF | `PNG` `JPG` `TIFF` | PDFKit / Poppler | 每页一个文件,输出到 `… pages` 文件夹 |
| 文档 `docx` `rtf` `odt` `txt` `md` `html` | `PDF` | AppKit 渲染器(macOS)· 其他系统 Pandoc | macOS 路径不需要 LaTeX 或 wkhtmltopdf |
| 文档 `docx` `rtf` `odt` `txt` `md` `html` | `PDF` | AppKit 渲染器(macOS)· 其他系统 Pandoc | macOS 路径不需要 LaTeX 或 wkhtmltopdf。带图片的 DOCX 会先经 Pandoc 转成 HTML,因为 AppKit 读取器会丢弃图片 |
| 文档 `docx` `rtf` `odt` `txt` `md` `html` | `DOCX` `RTF` `HTML` `TXT` `MD` | Pandoc | 自动省略源格式;Markdown 以 GFM 写出。HTML 为图片内嵌的独立单文件 |
| 演示文稿 `.pptx` | `MD` `PDF` | 纯 Rust + Pandoc | 按页生成标题、要点文字,**表格会输出为真正的 Markdown 表格**;PDF 仍是文字摘要版,非版面还原 |
| 电子表格 | `MD` `PDF` | `calamine` / 纯 Rust | 每个工作表一张 Markdown 表;`csv`/`tsv` 直接解析 |

View File

@ -189,6 +189,45 @@ do {
fail("could not parse document: \(error.localizedDescription)")
}
/// Fits every picture into the width of the printed page.
///
/// The layout sizes an attachment from the density header inside the file —
/// 72 dpi is assumed when the file carries none — and the cell AppKit builds
/// for an `<img>` honours neither `bounds` nor a CSS width, so a 1600 px
/// screenshot saved at 96 dpi asks for 1200 points and prints as a cropped
/// fragment of itself. A plain `NSTextAttachment` does honour `bounds`, and the
/// print operation then scales it onto the page along with the text. Aspect
/// ratio is kept and nothing is resampled: only the size the layout asks for
/// changes.
func fitAttachments(
_ source: NSAttributedString,
to containerWidth: CGFloat
) -> NSMutableAttributedString {
let mutable = NSMutableAttributedString(attributedString: source)
let wanted = max(1, containerWidth)
mutable.enumerateAttribute(
.attachment,
in: NSRange(location: 0, length: mutable.length),
options: []
) { value, range, _ in
guard let old = value as? NSTextAttachment,
let contents = old.fileWrapper?.regularFileContents,
let image = NSImage(data: contents),
image.size.width > wanted else { return }
let scale = wanted / image.size.width
let replacement = NSTextAttachment()
replacement.image = image
replacement.bounds = NSRect(
x: 0,
y: 0,
width: (image.size.width * scale).rounded(),
height: (image.size.height * scale).rounded()
)
mutable.addAttribute(.attachment, value: replacement, range: range)
}
return mutable
}
let printInfo = NSPrintInfo.shared.copy() as! NSPrintInfo
printInfo.paperSize = NSSize(width: 595.2, height: 841.8)
printInfo.topMargin = 42
@ -203,6 +242,7 @@ printInfo.jobDisposition = .save
printInfo.dictionary()[NSPrintInfo.AttributeKey.jobSavingURL] = output
let printable = printInfo.imageablePageBounds
let fitted = fitAttachments(attributed, to: printable.width)
let textView = NSTextView(frame: NSRect(x: 0, y: 0, width: printable.width, height: 1))
textView.isEditable = false
textView.isSelectable = false
@ -214,7 +254,7 @@ textView.textContainer?.containerSize = NSSize(
width: printable.width,
height: .greatestFiniteMagnitude
)
textView.textStorage?.setAttributedString(attributed)
textView.textStorage?.setAttributedString(fitted)
guard let layoutManager = textView.layoutManager,
let textContainer = textView.textContainer else {

View File

@ -962,10 +962,28 @@ fn native_document_pdf(
let extension = extension_of(input);
let mut source = input.to_path_buf();
let mut temporary = None;
// A DOCX with pictures cannot reach the AppKit reader: it silently drops
// every inline drawing, so the PDF keeps the text and loses the figures.
// Pandoc reads the same file as HTML with the media extracted next to it.
let mut work_dir = None;
if extension == "docx" && office::contains_inline_images(input).unwrap_or(false) {
let work = std::env::temp_dir().join(format!("shift-docx-html-{}", uuid::Uuid::new_v4()));
if fs::create_dir_all(&work).is_ok() {
if let Some(html) = docx_html_via_pandoc(input, &work) {
source = html;
work_dir = Some(work);
} else {
let _ = fs::remove_dir_all(work);
}
}
}
let mut base_dir = input
.parent()
.unwrap_or_else(|| Path::new("."))
.to_path_buf();
if let Some(work) = work_dir.as_ref() {
base_dir = work.clone();
}
if matches!(extension.as_str(), "md" | "markdown") {
// Markdown must never reach the renderer as plain text: without that
// step the PDF is a printout of the markup. Pandoc is nicer when it is
@ -1011,6 +1029,9 @@ fn native_document_pdf(
if let Some(path) = temporary {
let _ = fs::remove_file(path);
}
if let Some(directory) = work_dir {
let _ = fs::remove_dir_all(directory);
}
if !status.success() || !output.exists() {
let _ = fs::remove_file(&output);
return Err("Could not render this document to PDF.".into());
@ -1018,6 +1039,34 @@ fn native_document_pdf(
Ok(Some(output))
}
/// Renders a DOCX into the HTML document the macOS printer prints from.
///
/// Pandoc's reader keeps the pictures, tables and heading levels; the body is
/// wrapped in the stylesheet the Markdown path already uses, and the media
/// stays as files in `work` rather than base64 inside the HTML, which would
/// bloat a document whose figures are photographs.
#[cfg(target_os = "macos")]
fn docx_html_via_pandoc(input: &Path, work: &Path) -> Option<PathBuf> {
let pandoc = resolve_tool("pandoc")?;
let html = work.join("document.html");
let status = background_command(&pandoc)
.arg(input)
.args(["-f", "docx", "-t", "html5"])
.arg("--extract-media")
.arg(work.join("media"))
.args(["-o", html.to_string_lossy().as_ref()])
.status()
.ok()?;
if !status.success() || !html.exists() {
return None;
}
let body = fs::read_to_string(&html).ok()?;
if fs::write(&html, crate::markdown::page(&body)).is_err() {
return None;
}
Some(html)
}
/// Converts through the built-in macOS text converter.
///
/// Pandoc is an optional tool, and machines without it used to fail every
@ -2432,6 +2481,130 @@ mod tests {
fs::remove_dir_all(dir).unwrap();
}
/// The bug this pins: AppKit's OOXML reader keeps the text of a DOCX and
/// silently drops its pictures, so a report with figures printed as a text
/// document with no figures at all. The PDF must carry the image.
#[cfg(target_os = "macos")]
#[test]
fn keeps_pictures_when_rendering_a_text_docx_to_pdf() {
if which::which("pandoc").is_err() {
return;
}
let dir = scratch_dir();
let picture = dir.join("figure.png");
sample_image(&picture);
// Pandoc writes the fixture so the DOCX is a file a real reader accepts,
// with text and a picture in the same body.
let markdown = dir.join("report.md");
fs::write(
&markdown,
format!(
"# Report\n\nArchitecture overview.\n\n![Figure]({})\n",
picture.display()
),
)
.unwrap();
let input = dir.join("report.docx");
let status = background_command("pandoc")
.arg(&markdown)
.args(["-o", input.to_string_lossy().as_ref()])
.status()
.unwrap();
assert!(status.success() && input.exists());
assert!(office::contains_inline_images(&input).unwrap());
let output = convert_document(&input, "pdf", None, None).unwrap();
assert!(
pdf::has_embedded_images(&output).unwrap(),
"the figure was dropped from {}",
output.display()
);
let text = pdf::text_of(&output).unwrap_or_default();
assert!(text.contains("Architecture overview"), "{text}");
fs::remove_dir_all(dir).unwrap();
}
/// The second half of the same bug. The layout sizes a picture from the
/// density header inside the file, so a screenshot saved at 96 dpi asks for
/// 1200 points and prints as a cropped fragment of itself. The renderer has
/// to fit it to the page; a Form XObject's `/BBox` reports that width
/// without needing the content stream decoded.
#[cfg(target_os = "macos")]
#[test]
fn fits_a_wide_picture_onto_the_page() {
use lopdf::Object;
if which::which("pandoc").is_err() {
return;
}
let dir = scratch_dir();
let picture = dir.join("screenshot.png");
// 1200 px, and 72 dpi is what the layout assumes for a file with none.
RgbaImage::from_pixel(1200, 400, Rgba([30, 120, 200, 255]))
.save_with_format(&picture, ImageFormat::Png)
.unwrap();
let markdown = dir.join("shot.md");
fs::write(
&markdown,
format!("# Shot\n\nbefore\n\n![S]({})\n\nafter\n", picture.display()),
)
.unwrap();
let input = dir.join("shot.docx");
assert!(background_command("pandoc")
.arg(&markdown)
.args(["-o", input.to_string_lossy().as_ref()])
.status()
.unwrap()
.success());
let output = convert_document(&input, "pdf", None, None).unwrap();
let document = lopdf::Document::load(&output).unwrap();
let page_id = *document.get_pages().values().next().expect("a page");
let page = document.get_object(page_id).unwrap().as_dict().unwrap();
let media = page.get(b"MediaBox").unwrap().as_array().unwrap();
let coordinate = |index: usize| match &media[index] {
Object::Integer(value) => *value as f64,
Object::Real(value) => f64::from(*value),
other => panic!("MediaBox entry is {other:?}"),
};
let page_width = coordinate(2) - coordinate(0);
let mut widest: f64 = 0.0;
for object in document.objects.values() {
let Object::Stream(stream) = object else {
continue;
};
if stream
.dict
.get(b"Subtype")
.ok()
.and_then(|name| name.as_name().ok())
!= Some(b"Form")
{
continue;
}
let Ok(bbox) = stream.dict.get(b"BBox").and_then(|value| value.as_array()) else {
continue;
};
let edge = |index: usize| match &bbox[index] {
Object::Integer(value) => *value as f64,
Object::Real(value) => f64::from(*value),
_ => 0.0,
};
widest = widest.max(edge(2) - edge(0));
}
assert!(
widest > 1.0,
"no placed picture found in {}",
output.display()
);
assert!(
widest <= page_width,
"picture placed {widest}pt wide on a {page_width}pt page"
);
fs::remove_dir_all(dir).unwrap();
}
#[cfg(target_os = "macos")]
#[test]
fn renders_presentations_to_pdf() {

View File

@ -301,6 +301,12 @@ pub fn to_html(markdown: &str) -> String {
}
flush_paragraph(&mut paragraph, &mut body);
page(&body)
}
/// Wraps an HTML body in the document every renderer path prints from, so a
/// Pandoc-produced body and a built-in Markdown body reach the page the same way.
pub fn page(body: &str) -> String {
format!(
"<!doctype html>\n<html>\n<head>\n<meta charset=\"utf-8\" />\n<style>\n{}\n</style>\n</head>\n<body>\n{}\n</body>\n</html>\n",
STYLE, body
@ -321,6 +327,7 @@ blockquote { margin: 0 0 12px; padding-left: 12px; border-left: 3px solid rgba(2
table { border-collapse: collapse; margin: 0 0 12px; }
th, td { border: 1px solid rgba(90, 60, 40, 0.25); padding: 6px 10px; text-align: left; }
th { background: rgba(120, 80, 55, 0.08); }
img { max-width: 100%; height: auto; }
hr { border: 0; border-top: 1px solid rgba(90, 60, 40, 0.2); margin: 18px 0; }
a { color: #c8541f; }
"#;

View File

@ -84,6 +84,23 @@ pub fn document_page_images(
Ok(Some(images))
}
/// Whether the document body embeds at least one image.
///
/// Decides whether a DOCX can be handed to the native renderer, which drops
/// every inline drawing, or has to travel through a reader that keeps them.
pub fn contains_inline_images(input: &Path) -> Result<bool, String> {
let file = fs::File::open(input).map_err(|error| error.to_string())?;
let mut archive =
ZipArchive::new(file).map_err(|error| format!("Could not read document: {error}"))?;
let mut xml = String::new();
archive
.by_name("word/document.xml")
.map_err(|error| format!("Could not read document body: {error}"))?
.read_to_string(&mut xml)
.map_err(|error| error.to_string())?;
Ok(!document_body_summary(&xml)?.0.is_empty())
}
fn document_image_links(xml: &str) -> Result<std::collections::BTreeMap<String, String>, String> {
let mut reader = XmlReader::from_str(xml);
let mut links = std::collections::BTreeMap::new();

View File

@ -178,7 +178,7 @@ fn document_pdf_component() -> RuntimeComponent {
"document-pdf",
"AppKit document renderer",
"documents",
"DOCX / RTF / ODT / TXT / MD / HTML -> PDF",
"DOCX / RTF / ODT / TXT / MD / HTML -> PDF (a DOCX with pictures also needs Pandoc, which keeps the images)",
"bundled",
true,
None,