0

I have the following code which should print pdf file with a text, but it printed empty pdf file. Do you know how to fix it?

from win32 import win32print


if __name__ == "__main__":

    printer_name = "Microsoft Print to PDF"

    print("Printer: %s" % (printer_name))

    hPrinter = win32print.OpenPrinter(printer_name)

    try:
        hJob = win32print.StartDocPrinter(hPrinter, 1, ("test.pdf", None, "RAW"))
        try:
            f = open("to_print\\document1.pdf", "rb")
            win32print.StartPagePrinter(hPrinter)
            win32print.WritePrinter(hPrinter, f.read())
            win32print.EndPagePrinter(hPrinter)
            f.close()
        finally:
            win32print.EndDocPrinter(hPrinter)
    finally:
        print("Printing: %s" % (hJob))
        win32print.ClosePrinter(hPrinter)

If ghostscript is needed it is OK. But, I need to check for statuses of printing as here

xralf
  • 3,312
  • 45
  • 129
  • 200
  • What makes you think that your printer can print a raw PDF document? – David Heffernan Feb 28 '20 at 14:37
  • @DavidHeffernan Should I use rather TEXT? I'm on Linux now, I can test it on monday. – xralf Feb 28 '20 at 15:05
  • 1
    You've asked a series of questions on this subject now, but you don't seem to be getting the hint. So far as I know you need a program that can display PDF in order to print it. Probably something like Ghostscript. Printers, by and large, don't know how to render PDF. And what about file formats other than PDF? – David Heffernan Feb 28 '20 at 15:10
  • @DavidHeffernan My input is a pdf file. If it will change in the future I don't know, so far. – xralf Feb 28 '20 at 15:19
  • @DavidHeffernan I could use shellexecute according to [this](http://timgolden.me.uk/python/win32_how_do_i/print.html), but I need to check the status of the document, that's because I'm using win32print. I have seen projects on github that use pdf with win32print, so I adjusted my code to it. – xralf Feb 28 '20 at 15:22
  • I think you need to step back and learn a bit about how printers work. – David Heffernan Feb 28 '20 at 15:30
  • @DavidHeffernan So, there is no possibility to put together win32print and some application that renders pdf (if win32print is not doing the rendering). If I understood you. I really need to check the statuses as [here](https://stackoverflow.com/questions/53765699/python-win32print-job-status) – xralf Feb 28 '20 at 15:32
  • 1
    I'm pretty sure that ghostscript can do this – David Heffernan Feb 28 '20 at 17:43
  • @DavidHeffernan I think, I've got it. I can use ghostscript and still check with win32print the job queue. I will try on monday. Thank you. – xralf Feb 28 '20 at 23:49
  • This doesn't make sense. You have pseudo code which attempts to print PDF file to a PDF printer. If successful, the result will be the same PDF file. What is your actual goal? – Barmak Shemirani Feb 29 '20 at 15:45
  • 1
    @BarmakShemirani To print a pdf file on physical printer. Now, I'm testing on "Microsoft Print to PDF" to save paper. – xralf Feb 29 '20 at 16:21
  • Hi, have your issue been solved? – Strive Sun Mar 09 '20 at 08:18
  • @StriveSun-MSFT Hi, it is solved as a combination of [this](https://stackoverflow.com/questions/15748386/how-to-catch-printer-event-in-python) and [this](https://stackoverflow.com/questions/39249360/python-print-pdf-file-with-win32print/60573860?noredirect=1) questions answers. I will delete this. – xralf Mar 09 '20 at 12:01

0 Answers0