Skip to content
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

Allow suppression of (cid:N) in pdf2txt #1070

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed

- Using absolute instead of relative imports ([[#995](https://github.com/pdfminer/pdfminer.six/pull/995)])
- Allow suppression of `(cid:N)` in `pdf2txt.py` ([#1070](https://github.com/pdfminer/pdfminer.six/pull/1070))

### Deprecated

Expand Down
Binary file added samples/contrib/issue-1056-cid.pdf
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/test_tools_pdf2txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ def test_contrib_issue_350(self):
"""
run("contrib/issue-00352-asw-oct96-p41.pdf")

def test_contrib_issue_1056(self):
"""Test fix to pdf2txt.py mentioned in
https://github.com/pdfminer/pdfminer.six/issues/1056"""
with TemporaryFilePath() as output_file_name:
pdf2txt.main(
[
"--ignore-unmapped",
f"-o{output_file_name}",
absolute_sample_path("contrib/issue-1056-cid.pdf"),
]
)
with open(output_file_name) as infh:
for spam in infh:
assert "(cid:" not in spam

def test_scancode_patchelf(self):
"""Regression test for https://github.com/euske/pdfminer/issues/96"""
run("scancode/patchelf.pdf")
Expand Down
11 changes: 11 additions & 0 deletions tools/pdf2txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any, Container, Iterable, List, Optional

import pdfminer.high_level
from pdfminer.converter import PDFLayoutAnalyzer
from pdfminer.layout import LAParams
from pdfminer.pdfexceptions import PDFValueError
from pdfminer.utils import AnyIO
Expand Down Expand Up @@ -277,6 +278,13 @@ def create_parser() -> argparse.ArgumentParser:
help="Remove control statement from text. "
"Only used when output_type is xml.",
)
output_params.add_argument(
"--ignore-unmapped",
"-I",
default=False,
action="store_true",
help="Ignore unmapped characters rather than outputting" "(cid:N) in the text",
)

return parser

Expand Down Expand Up @@ -309,6 +317,9 @@ def parse_args(args: Optional[List[str]]) -> argparse.Namespace:
if parsed_args.outfile.endswith(override):
parsed_args.output_type = alttype

if parsed_args.ignore_unmapped:
PDFLayoutAnalyzer.handle_undefined_char = lambda *args: "" # type: ignore

return parsed_args


Expand Down
Loading