Skip to content

BUG: Title sometimes is bytes and not str. #2930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2024

Conversation

reformy
Copy link
Contributor

@reformy reformy commented Nov 1, 2024

closes #2929

@reformy reformy force-pushed the title-as-bytes branch 3 times, most recently from 130c520 to 9c289d7 Compare November 1, 2024 01:05
Copy link

codecov bot commented Nov 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (main@5b50f47). Learn more about missing BASE report.
Report is 113 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2930   +/-   ##
=======================================
  Coverage        ?   96.40%           
=======================================
  Files           ?       52           
  Lines           ?     8744           
  Branches        ?     1591           
=======================================
  Hits            ?     8430           
  Misses          ?      186           
  Partials        ?      128           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@reformy reformy force-pushed the title-as-bytes branch 3 times, most recently from c888a42 to c31933b Compare November 1, 2024 02:55
@stefan6419846
Copy link
Collaborator

Thanks for the PR. I assume that you own all the necessary copyrights for the test file?

For me this mostly looks okay, apart from the review comment left above about the position after the comment. Nevertheless, I am going to wait for @pubpub-zz here to have another look.

@pubpub-zz
Copy link
Collaborator

Personnaly I would have prefer to add the str to the BytesStringObject() with trying diffrerent encoding(see §3.8.1) .

we should use CHARSET (

CHARSETS = ("utf-8", "gbk", "latin1")
) adding as first format UTF-16 (based on the spec)

It would then just require to add a call to str in the diffrerent properties of metadata in https://github.com/py-pdf/pypdf/blob/main/pypdf/_doc_common.py

@reformy
Copy link
Contributor Author

reformy commented Nov 2, 2024

Thank you for your comments.
I've moved the utf-8 to be after the pdfdocencoding.
@pubpub-zz I did not understand your comment. If the whole str-decoding is not in the right place, that a different issue. I've just added another option to decode it.

@stefan6419846
Copy link
Collaborator

If the whole str-decoding is not in the right place, that a different issue.

No, this is related to this issue. The suggestion is to revert the current changes and provide ByteStringObject.__str__() accordingly, which would then test the different charsets. At the same time, DocumentInformation._get_text() should be rewritten to check for StringObject and ByteStringObject and - instead of returning the pure value - return str(retval).

@reformy
Copy link
Contributor Author

reformy commented Nov 6, 2024

I've changed the code as suggested, adding __str__ and calling it from _get_text. This works well.

However, adding utf-16 to the CHARSETS list breaks other things. I don't understand why do we need this and how is this related to my fix.

@stefan6419846
Copy link
Collaborator

However, adding utf-16 to the CHARSETS list breaks other things. I don't understand why do we need this and how is this related to my fix.

This is another encoding to consider. You should only consider this in __str__, not add it to the list permanently.

@reformy reformy force-pushed the title-as-bytes branch 5 times, most recently from 356e8a3 to 5a1c1fc Compare November 6, 2024 07:56
@stefan6419846 stefan6419846 merged commit e2ebf10 into py-pdf:main Nov 8, 2024
16 checks passed
@reformy reformy deleted the title-as-bytes branch November 10, 2024 08:35
stefan6419846 added a commit that referenced this pull request Jan 26, 2025
## What's new

### Deprecations (DEP)
- Deprecate with replacement CCITParameters (#3019) by @j-t-1
- Correct deprecation of interiour_color (#2947) by @j-t-1

### New Features (ENH)
- Support alternative (U)F names for embedded file retrieval (#3072) by @stefan6419846
- Adding support for reading .metadata.keywords (#2939) by @Lucas-C

### Bug Fixes (BUG)
- Handle further Tf operators in text extraction layout mode (#3073) by @blushingpenguin
- Ensure `add_metadata` can deal with `_info = None` (#3040) by @xmo-odoo
- Handle IndirectObject in CCITTFaxDecode filter (#2965) by @stefan6419846
- Handle chained colorspace for inline images when no filter is set (#3008) by @stefan6419846
- Avoid extracting inline images twice and dropping other operators (#3002) by @stefan6419846
- Fixed reference of value with `str.__new__` in TextStringObject (#2952) by @thomas-forte
- Handle indirect objects in font width calculations (#2967) by @nsw42
- Title sometimes is bytes and not str (#2930) by @reformy
- Fix undefined variable for text extraction (regression) (#2934) by @stefan6419846
- Don't close stream passed to PdfWriter.write() (#2909) by @alexaryn

### Robustness (ROB)
- Handle zero height fonts when extracting text (#3075) by @blushingpenguin
- Deal with content streams not containing streams (#3005) by @stefan6419846
- Gracefully handle some text operators when the operands are missing (#3006) by @stefan6419846
- Fall back to non-Adobe Ascii85 format for missing end markers (#3007) by @stefan6419846
- Ignore odd-length strings when processing cmap lines (#3009) by @stefan6419846
- Skip annotation destination being NullObject in PdfWriter (#2964) by @stefan6419846
- Skip destination page being None in PdfWriter (#2963) by @dxsooo
- Fix infinite loop case when reading null objects within an Array by @jakep-allenai
- Fixing infinite loop in ArrayObject read_from_stream (#2928) by @jakep-allenai

### Documentation (DOC)
- Add note about default line colors (#3014) by @stefan6419846

### Developer Experience (DEV)
- Remove ignoring Ruff rule PGH004 (#3071) by @j-t-1
- Tidy ignore array in tool.ruff.lint (#3069) by @j-t-1
- Move Windows CI to Python 3.13 (#3003) by @stefan6419846
- Move to Ubuntu 22.04 (#3004) by @stefan6419846

### Maintenance (MAINT)
- Fix formatting of warning message and include exception message (#3076) by @stefan6419846
- Narrow return type for `ContentStream.operations` (#2941) by @kmurphy4

### Testing (TST)
- Fix image similarity for upcoming Ubuntu 24.04 (#3039) by @stefan6419846
- Replace broken Apache Tika Corpora urls (#3041) by @stefan6419846

### Code Style (STY)
- Add form feed to WHITESPACES (#3054) by @j-t-1
- Lots of small internal changes by @j-t-1

[Full Changelog](5.1.0...5.2.0)

Signed-off-by: stefan6419846 <96178532+stefan6419846@users.noreply.github.com>
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.

DocumentInformation.title sometimes return bytes instead of str
3 participants