Repository navigation
feat(pdf): Grayscale turns a PDF gray with Ghostscript (#1930) - #1976
Open
cre8ive-marketing wants to merge 4 commits into
Open
cre8ive-marketing wants to merge 4 commits into
cre8ive-marketing wants to merge 4 commits into
Conversation
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
KnifeLemon
requested changes
Dec 2, 2026
KnifeLemon
left a comment
Owner
There was a problem hiding this comment.
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:
- 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 toPasswordProtected(). A small test would help. docs/ENGINES.md: the Ghostscript row lists what it's used for. Please add grayscale PDFs.- 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. - Nits:
- In
GhostscriptConverter.csthe header rewrap leaves// The job queue has every PDF aon a line of its own (line 7). GrayPdfAsyncruns withprogress: null, so a big file sits at 0% during the gray pass.AssertGraychecks that the note is still an annotation, but not that it's gray. pdfwrite may keep its/Ccolour as RGB, so a viewer could still draw it red. Could you assert the annotation's colour too?ko.json: the othererror.*_needs_*keys end in "설치해 주세요", and the new one ends in "설치하세요".
- In
…e-pdf # Conflicts: # CHANGELOG.md
- 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
approved these changes
Dec 2, 2026
KnifeLemon
left a comment
Owner
There was a problem hiding this comment.
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.
# Conflicts: # CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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).PdfSharpConverter.ConvertAsyncmakes the gray file first, then runs the same branch on it (step with { InputPath = gray }). SoSheets,WithoutMetadata,SplitOrExtractandCompressAsyncall work on the gray file.CompressFileAsynconly ever sees the gray file, so a colour candidate can't win as the smallest.ConversionRefusedException("error.grayscale_needs_ghostscript"), likeerror.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)DeviceGray. The text still reads and the note is still an annotation.Compression = Ebook.Not checked
PdfGrayscaleTests, not the full suite.ConvertAsync.