2

I have the code below which creates a pdf file in memory.

 pdf = pytesseract.image_to_pdf_or_hocr(self.main_window.filename, extension='pdf')

I want to save that pdf file in disk so that I can view it. How will I do it in python?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
alyssaeliyah
  • 2,214
  • 6
  • 33
  • 80
  • Possible duplicate of [Download and save PDF file with Python requests module](https://stackoverflow.com/questions/34503412/download-and-save-pdf-file-with-python-requests-module) – PV8 Jun 17 '19 at 06:58
  • https://stackoverflow.com/questions/34503412/download-and-save-pdf-file-with-python-requests-module – PV8 Jun 17 '19 at 06:58
  • @PV8 It wasnt a duplicate. I dont have to download a pdf, I just have to save it. – alyssaeliyah Jun 17 '19 at 07:03
  • https://stackoverflow.com/questions/2252726/how-to-create-pdf-files-in-python – PV8 Jun 17 '19 at 07:05
  • 1
    What is `type(pdf)`? If it is of type `` then you can just use the standard file write method. –  Jun 18 '19 at 06:05
  • @JustinEzequiel Yes it is of class bytes. how to do it? I tried f = open("pdf.pdf", "w+") f.write(pdf) f.close() – alyssaeliyah Jun 19 '19 at 07:02
  • @JustinEzequiel - Thank you. I made it with f = open("pdf.pdf", "wb") f.write(pdf) f.close() . You can put your answer at the answer section so that I can mark it as the right answer. – alyssaeliyah Jun 19 '19 at 07:07

1 Answers1

2

If type(pdf) is of type <class 'bytes'> then you can just do:

with open('yourfile.pdf', 'wb') as f:
    f.write(pdf)