Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

PDFDocument is slow, any way to speed it up? #283

Open
typhoon71 opened this issue Jan 6, 2020 · 4 comments
Open

PDFDocument is slow, any way to speed it up? #283

typhoon71 opened this issue Jan 6, 2020 · 4 comments

Comments

@typhoon71
Copy link

typhoon71 commented Jan 6, 2020

I'm currently doing this:

fp = open(pdf_fpath, 'rb')
parser = PDFParser(fp)
doc = PDFDocument(parser)

With the pdfs I'm working with, PDFDocument is really too slow. Is there any way to speed it up?

I see that there's often the suggestion to "giving -n option which turns off automatic layout analysis.", but I have no idea how to do that from python.

I "only" need to read xrefs and objects/streams, I don't care how the pdf would be rendered/pages.
Can anyone help please? Thanks.

@typhoon71
Copy link
Author

From that example:

parser = PDFParser(in_file)
doc = PDFDocument(parser)

was exactly where I timed the slowdown.

 doc = PDFDocument(parser)

is time consumnig, like 99%.

If I understand your suggestion, I should make my own version of PDFDocument?
One that does not analyze the layout?

No time for it right now, but maybe I could in the future.

Actually I hoped in some kind of switch for PDFDocument... but I found nothing.

@pietermarsman
Copy link

I'm sorry. My comment was short, not clear and partially wrong. I've deleted it.

Layout analysis is not used during the parsing of the PDF. It is only used when you interpret the document, with e.g. TextConverter of HTMLConverter.

Could you share an example PDF that PDFDocument is really slow on?

@typhoon71
Copy link
Author

Sadly I can't share those PFDs.
The thing is that once I got PDFDocument initialized, everything else is really fast.
Maybe it's decompressing the PDF, that could be slowing it.

@hiDaDeng
Copy link

import fitz

filepath = "C:\\user\\docs\\aPDFfile.pdf"

text = ''
with fitz.open(filepath ) as doc:
    for page in doc:
        text+= page.get_text()
print(text)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants