Skip to content

feat(pdf): Grayscale turns a PDF gray with Ghostscript (#1930) - #1976

Open
cre8ive-marketing wants to merge 4 commits into
KnifeLemon:mainfrom
cre8ive-marketing:feature/1930-grayscale-pdf
Open

cre8ive-marketing wants to merge 4 commits into
KnifeLemon:mainfrom
cre8ive-marketing:feature/1930-grayscale-pdf

Conversation

@cre8ive-marketing

Copy link
Copy Markdown
Collaborator

Closes #1930

What changes

The Grayscale preset on a PDF used to give the PDF back in colour. Now Ghostscript's pdfwrite rewrites the whole file in gray first (GhostscriptConverter.GrayPdfAsync: -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray -dAutoRotatePages=/None).

  • Every branch: PdfSharpConverter.ConvertAsync makes the gray file first, then runs the same branch on it (step with { InputPath = gray }). So Sheets, WithoutMetadata, SplitOrExtract and CompressAsync all work on the gray file.
  • Compression: CompressFileAsync only ever sees the gray file, so a colour candidate can't win as the smallest.
  • Kept: text stays text, vectors stay vectors, annotations stay annotations. No page is turned into a picture.
  • No Ghostscript: ConversionRefusedException("error.grayscale_needs_ghostscript"), like error.upscale_needs_engine. The text points to Settings → Engines, in all 23 i18n files.

PDF/A

The PDF/A step still turns colours into RGB for the sRGB output intent, so the operators aren't gray any more. The page still looks gray, though: the test renders it and checks R = G = B (within 3 levels).

Order

The gray pass runs on the whole file before the page range or split, not after as in my plan. That's one Ghostscript run instead of one per part, and the code doesn't change in four places. The cost is that a page range of a big PDF grays pages it then drops.

Tests (PdfGrayscaleTests)

  • Test file: a PDF built in the test, with red text, a blue bar, an RGB JPEG and a red note annotation.
  • Plain: after Grayscale, letters, fills and the picture are DeviceGray. The text still reads and the note is still an annotation.
  • Compressed: the same checks with Compression = Ebook.
  • PDF/A: rendered to PNG, every pixel is gray.
  • Without Ghostscript: the job fails with the new key. It skips here because Ghostscript is installed, the other way round from the other three.
  • Bug check: with the gray branch switched off, the three Ghostscript tests fail.

Not checked

  • I ran the Engines PDF tests and PdfGrayscaleTests, not the full suite.
  • A merged PDF ("Merge PDF" + Grayscale) isn't covered: the merge path doesn't go through ConvertAsync.

The Grayscale preset on a PDF used to give the PDF back in colour.
Ghostscript's pdfwrite now rewrites the file in gray first (text,
vectors and annotations kept), and sheets, metadata removal, page
ranges, splitting and compression work on that gray file. Without
Ghostscript the file fails with error.grayscale_needs_ghostscript.

Closes KnifeLemon#1930
@github-actions github-actions Bot added the feature New features label Dec 2, 2026
@cre8ive-marketing
cre8ive-marketing marked this pull request as ready for review December 2, 2026 04:31

@KnifeLemon KnifeLemon left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @cre8ive-marketing, this follows the plan closely, and the PDF/A render check in the tests is a nice touch.

Order: graying the whole file once, before the page range or split, is fine with us. It's one Ghostscript run, and the four branches stay as they are.

A few things before merge:

  1. Password-protected PDFs. Today a locked PDF on the Grayscale preset fails with the clear "The PDF is password-protected…" message (PdfDocumentReader.PasswordProtected()). Now Ghostscript reads it first, and without a password it fails with a raw "Ghostscript failed (exit 1) …" instead. Could you keep the clear message? Either check the file with PDFsharp first, or map that Ghostscript failure to PasswordProtected(). A small test would help.
  2. docs/ENGINES.md: the Ghostscript row lists what it's used for. Please add grayscale PDFs.
  3. Merge PDF + Grayscale doesn't go through ConvertAsync, so a merged PDF still comes out in colour. That can be a follow-up, but please say so in the CHANGELOG bullet.
  4. Nits:
    • In GhostscriptConverter.cs the header rewrap leaves // The job queue has every PDF a on a line of its own (line 7).
    • GrayPdfAsync runs with progress: null, so a big file sits at 0% during the gray pass.
    • AssertGray checks that the note is still an annotation, but not that it's gray. pdfwrite may keep its /C colour as RGB, so a viewer could still draw it red. Could you assert the annotation's colour too?
    • ko.json: the other error.*_needs_* keys end in "설치해 주세요", and the new one ends in "설치하세요".

- A locked PDF fails with the "password-protected" message again: it is
  opened with PDFsharp before Ghostscript runs (test added).
- Annotation /C and /IC colours left in RGB by pdfwrite become gray; the
  test checks the note's colour.
- The gray pass reports progress (first 80 %).
- docs/ENGINES.md: Ghostscript row lists gray PDFs. CHANGELOG says Merge
  PDFs + Grayscale stays in colour for now.
- GhostscriptConverter header comment rewrapped; ko.json wording.

@KnifeLemon KnifeLemon left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @cre8ive-marketing, all addressed: the locked-PDF check before Ghostscript (with a test), the note colours, progress, the header comment and ENGINES.md. We'll merge once CI is green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: proper grayscale (black & white) PDF for PDF → PDF conversion

2 participants