Skip to content

fix: fails to decode PDFDocEncoding-encoded Strings in metadata#477

Merged
J-F-Liu merged 2 commits intoJ-F-Liu:mainfrom
TPKato:main
Mar 13, 2026
Merged

fix: fails to decode PDFDocEncoding-encoded Strings in metadata#477
J-F-Liu merged 2 commits intoJ-F-Liu:mainfrom
TPKato:main

Conversation

@TPKato
Copy link
Copy Markdown
Contributor

@TPKato TPKato commented Mar 12, 2026

Related to: #459.
May also be related to #110.

Problem

The Document::load_metadata() function does not decode the string correctly if it is encoded using PDFDocEncoding.

According to PDF 1.7, Section 14.3.3, the type of title, author, etc., in the document information dictionary shall be a text string. That is,

bytes that represent characters that shall be encoded using either PDFDocEncoding or UTF-16BE with a leading byte-order marker
(PDF 1.7, Section 7.9.1)

Fix

use common_data_structures::decode_text_string() to decode strings (#297)

Example

Example PDF with a PDFDocEncoding-encoded document information dictionary:
http://mirrors.ctan.org/info/l2tabu/german/l2tabu.pdf

Code for test:

use std::path::Path;
use lopdf::Document;

fn main() {
    let file = "l2tabu.pdf";
    let m = Document::load_metadata(Path::new(file)).expect("load_metadata failed");
    println!("Title:  {:?}", m.title.unwrap());
    // println!("Author: {:?}", m.author.unwrap());

Result: (lopdf = "0.39.0")

Title:  "Das LaTeX2e-S�ndenregister oder Veraltete Befehle, Pakete und andere Fehler"

Expected/Fixed:

Title:  "Das LaTeX2e-Sündenregister oder Veraltete Befehle, Pakete und andere Fehler"

Disclaimer

I am a complete beginner to Rust, so I'm unsure if this patch is appropriate. Please feel free to rewrite it if necessary.

@J-F-Liu
Copy link
Copy Markdown
Owner

J-F-Liu commented Mar 12, 2026

Thanks, please change Some(common_data_structures::decode_text_string(obj).unwrap()) to common_data_structures::decode_text_string(obj).ok()

@J-F-Liu J-F-Liu merged commit 087d3bc into J-F-Liu:main Mar 13, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants