Convert pdf to monochrome black-and-white via command line

10,900

Solution 1

Adapting this answer over on SuperUser, this can be achieved by converting the PDF to PostScript and back using a redefined setrgbcolor command:

gs -o <output-file.pdf> -sDEVICE=pdfwrite \
-c "/osetrgbcolor {/setrgbcolor} bind def /setrgbcolor {pop [0 0 0] osetrgbcolor} def" \
-f <input-file.ps>

Solution 2

This answer given at superuser worked for me, I did not get other answers on this question working within 2 minutes. Credits go to goyinux for the original answer.

gs \
 -sOutputFile=output.pdf \
 -sDEVICE=pdfwrite \
 -sColorConversionStrategy=Gray \
 -dProcessColorModel=/DeviceGray \
 -dCompatibilityLevel=1.4 \
 -dNOPAUSE \
 -dBATCH \
 input.pdf

Solution 3

This is what worked for me in Mint 20.04 to darken PDF text while trying to avoid the relatively heavy Adobe product.

To take a PDF with gray text and make it easier to read:

  1. Convert the PDF to individual image files:

    pdftoppm input_file.pdf output_file -png -rx 300 -ry 300
    

    where -rx 300 -ry 300 is the DPI.

  2. Export your PDF pages to image files, and then batch process them.

    Install XnConvert using its .deb package or using Flatpak. Add the PDF in the first tab. Go to AddMapBlack/White Points. White Points should be higher than Black Points. Put them at like 190 and 127 respectively (adjust from there).

    In the output tab use the following:

    • Filename: yourfilename
    • Format: PDF
    • Quality: 99
    • Multipage: Convert multipage file to multipage file.
  3. Install gscan2pdf and open the application. Drag and drop the selected batch of files from the file manager to the vertical bar on the left of the gscan2pdf app. FileSave, select Downsample to 150 PPI, select a name for your new PDF with blacker text.


I suspect the Adobe reader approach will work too, but here is a version without the Adobe product. On the upside, XnConvert seems quite flexible and lightweight. On the downside I don't think XnConvert is open source either.

Share:
10,900

Related videos on Youtube

Earthliŋ
Author by

Earthliŋ

Updated on September 18, 2022

Comments

  • Earthliŋ
    Earthliŋ over 1 year

    I have a 65-page PDF file that has text in a dark grey. (Actual text, not a picture/image of text.) Printing on my B/W printer, the text gets all blurry, because the printer is trying to make it less than black. I can solve this problem page-per-page by importing the pages to inkscape, selecting all paths, clicking on "Black", which converts all colours including the greytones to pure black, and exporting back to PDF.

    Is there a way to do these steps in inkscape in the command line (for batch processing)?

    Is there a way to achieve the same result (convert pdf to black and white, 1-bit greyscale) without inkscape?


    Edit. There are similar questions on Unix.SE and on superuser, which use Ghostscript. However, these only convert the files to greyscale via the option -sColorConversionStrategy=Gray. The option -sColorConversionStrategy=/Mono results in the error Unrecoverable error: rangecheck in .putdeviceprops. Since my file is greyscale already, these don't solve my problem.

    • Byte Commander
      Byte Commander about 8 years
      Would it necessarily have to be an inkscape command or are other PDF conversion tools also okay? (I don't know how to do it, I'm just trying to improve the question)
    • Earthliŋ
      Earthliŋ about 8 years
      @ByteCommander Other PDF conversion tools would be okay as well.
    • Jacob Vlijm
      Jacob Vlijm about 8 years
      Your pdf file, is it textual or image (i.o.w. is the text selectable or not)?
    • Earthliŋ
      Earthliŋ about 8 years
      @JacobVlijm It's text, not an image. (It contains a couple of figures, which I don't need to convert to B/W. I care primarily about the readability of the text.)
    • Earthliŋ
      Earthliŋ about 8 years
      @JacobVlijm Thank you for the link. As also the answers say, the command doesn't work with the Mono option for Ghostscript, only with Grey. My text is grey already, I need to make it black. Those answers unfortunately don't solve my problem.
  • XavierStuvw
    XavierStuvw about 3 years
    Does not work with gs 9.26. The document remains in colour.
  • Earthliŋ
    Earthliŋ about 3 years
    @XavierStuvw Still works for me with gs 9.50 on embedded text, vector graphics and images. Did you use a PostScript file as input? How are you checking the output?
  • XavierStuvw
    XavierStuvw about 3 years
    Yes, I used a ps as input; would you please add the line that converts the original pdf into a ps for good measure? What do you mean for 'checking the output'? NB My document is scanned book, and each page appears to be an image.
  • Earthliŋ
    Earthliŋ about 3 years
    I just use the "Print to File" option in the "Print" dialog, selecting PS as filetype. I don't know what command this uses under the hood, but I get similar results with pdf2ps which seems to use GhostScript and it doesn't work with PS files generated by pdftops which uses Poppler (leaving colours as they are). The reason I was asking how you check is that I used to use Evince for checking if the conversion from a dark grey to true black worked, but some versions of Evince used to display true black #000000 in PDF files as something like #111111 (presumably to be "screen-friendly").
  • Earthliŋ
    Earthliŋ about 3 years
    If pdf2ps works also for you, I'll add this to my answer, but a scanned book should probably be converted to greyscale rather than pure B/W using something like this answer on superuser...? @XavierStuvw
  • xeruf
    xeruf over 2 years
    note that omitting -png makes pdftoppm significantly faster, in case you don't specifically want a png. Also -progress is handy.
  • Tom
    Tom over 2 years
    It may cause a visible loss in quality