0

My task is to print PDF file using Windows OS and Python. So when I print through my HP printer I get the exact copy of that PDF, but with the gray color background.

I generate PDF using PyFPDF library. It works fine. Here is the output of the PDF file. If I print through Adobe Acrobat Reader, there is no gray color background. But when I print using ghostscript, it's comes with gray color background.

import subprocess
import win32print

generated_pdf_name = 'some_pdf_name.pdf'
current_printer = win32print.GetDefaultPrinter()

query = f'{os.path.join("GHOSTSCRIPT", "bin", "gswin64c.exe")} -dPrinted -dBATCH -dNOPAUSE -dFitPage -q -dNumCopies=1 -dNoCancel -sDEVICE=mswinpr2 -dDeviceGrayToK=false -sOutputFile="%printer%{current_printer}" "{os.path.join(generated_pdf_name)}"'
subprocess.call(query, shell=True)

Here I used MS Windows printers, so -sDEVICE=mswinpr2. I also tried with gsprint. But it doesn't show images in my PDF file. So that's I moved into ghostscript. It printed all the content of this PDF file, but only problem is the gray color background.

Take a look Gray color printed output, I need plain output without gray background. Did I miss anything above?

1 Answers1

0

Looks like your 'PDF file' is nothing but a scanned image, but since you haven't supplied the actual PDF file its impossible to tell.

My guess (that's al lit can be without seeing the original PDF) is that the entire content is an image, and the 'white background' is not truly white, its 'nearly white'.

The mswinpr2 device works by rendering the PDF file to a bitmap, and then sending that bitmap to the printer (Acrobat Reader works in a quite different way). I would guess that rendering the 'not quite white' to RGB makes it even less white and this results in the background becoming slightly more gray, to the point where, when printed, it is visibly gray.

There are, essentially, two options; you could fix the original PDF file so that the background is truly white or you could use a different ICC profile for the rendering, which would result in a different colour being rendered for the background.

Using a different ICC profile is covered in the Ghostscript documentation.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • Hello, thanks for the answer. Here you can get my generated [PDF File](https://gofile.io/?c=RbDzfV). Please check it and let me know. When I print without `ghostscript`, it prints nicely. – ghost rider Sep 17 '19 at 07:58
  • So the file isn't a scanned image. When I render it with Ghostscript it renders an apparently white background. When I run it through mswinpr2 and point it to a printer, I get a white background. Checking the actual bitmap I find that the white background is 'not quite white', instead of 0xFF 0xFF 0xFF its 0xFE 0xFE 0xFE. I wouldn't have expected that to be different enough to trigger a visible gray though. What version of GS are you using ? – KenS Sep 17 '19 at 10:17
  • It's the latest version, `gs9.27`. I'll check the background color. But did you print it without using `mswinpr2`? I printed it via `Adobe Acrobat Reader DC` software, then I got clear white output. How it's happen? – ghost rider Sep 17 '19 at 11:02
  • I printed it using mswinpr2. As I said, Acrobat probably uses a quite different route for its printing, it will be essentially the same as it uses for screen display, which is not what Ghostscript's mswinpr2 device does. – KenS Sep 17 '19 at 11:11