pythonpython-3.xpypdf

Is it possible to input pdf bytes straight into PyPDF2 instead of making a PDF file first


I am using Linux; printing raw to port 9100 returns a "bytes" type. I was wondering if it is possible to go from this straight into PyPDF2, rather than make a pdf file first and using method PdfFileReader?

Thank you for your time.


Solution

  • PyPDF2.PdfFileReader() defines its first parameter as:

    stream – A File object or an object that supports the standard read and seek methods similar to a File object. Could also be a string representing a path to a PDF file.

    So you can pass any data to it as long as it can be accessed as a file-like stream. A perfect candidate for that is io.BytesIO(). Write your received raw bytes to it, then seek back to 0, pass the object to PyPDF2.PdfFileReader() and you're done.