Skip to content

fix(high_level): gracefully handle subset_fonts failures#1126

Open
octo-patch wants to merge 1 commit intoPDFMathTranslate:mainfrom
octo-patch:fix/issue-975-subset-fonts-error-handling
Open

fix(high_level): gracefully handle subset_fonts failures#1126
octo-patch wants to merge 1 commit intoPDFMathTranslate:mainfrom
octo-patch:fix/issue-975-subset-fonts-error-handling

Conversation

@octo-patch
Copy link
Copy Markdown
Contributor

Fixes #975

Problem

When doc.subset_fonts(fallback=True) is called during PDF generation, PyMuPDF may raise ValueError: bad 'value' for certain PDFs that have malformed font width tables. This causes the entire translation to crash at the very end — after all translation work has already completed — producing no output file.

Stack trace from the issue:

File "pymupdf/utils.py", line 5848, in subset_fonts
    set_old_widths(font_xref, width_table, def_width)
File "pymupdf/utils.py", line 5617, in set_old_widths
    doc.xref_set_key(df_xref, "DW", dwidths)
ValueError: bad 'value'

Solution

Wrap each subset_fonts() call in a try-except block. If font subsetting fails, log a warning and continue — the output PDF is still written successfully. The only downside is a slightly larger file size when font subsetting is skipped.

Testing

  • Verified the fix allows translation to complete when subset_fonts raises a ValueError
  • Existing behavior is preserved for PDFs where font subsetting succeeds

When PyMuPDF's subset_fonts() raises a ValueError (e.g. due to a bad
font width table value in some PDFs), the translation would crash after
completing all the actual translation work. Wrap each subset_fonts call
in a try-except so the output PDF is still written even if font
subsetting fails. The output will be slightly larger in this case.

Fixes PDFMathTranslate#975
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.

报错:ValueError: bad 'value'

1 participant