feat(docs): optimize PDF via Ghostscript pdfwrite
Add oo docs optimize for native-text PDFs (OO-friendly rewrite without ocrmypdf overlay). Default ocr --force to false when text layer exists. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
// - ocrmypdf — OCR into a searchable PDF
|
||||
// - pdftotext — extract text layer
|
||||
// - tesseract — OCR single images when ocrmypdf is unsuitable
|
||||
// - ghostscript (gs) — PDF rewrite/optimize via PostScript (pdfwrite)
|
||||
package docpipe
|
||||
|
||||
import (
|
||||
@@ -22,10 +23,11 @@ const DefaultMinTextChars = 200
|
||||
|
||||
// Tools reports which converters are available on PATH.
|
||||
type Tools struct {
|
||||
Pandoc string
|
||||
OCRMyPDF string
|
||||
PDFToText string
|
||||
Tesseract string
|
||||
Pandoc string
|
||||
OCRMyPDF string
|
||||
PDFToText string
|
||||
Tesseract string
|
||||
Ghostscript string
|
||||
}
|
||||
|
||||
// LookPath resolves converter binaries (empty string if missing).
|
||||
@@ -39,10 +41,11 @@ func LookPath() Tools {
|
||||
return ""
|
||||
}
|
||||
return Tools{
|
||||
Pandoc: find("pandoc"),
|
||||
OCRMyPDF: find("ocrmypdf"),
|
||||
PDFToText: find("pdftotext"),
|
||||
Tesseract: find("tesseract"),
|
||||
Pandoc: find("pandoc"),
|
||||
OCRMyPDF: find("ocrmypdf"),
|
||||
PDFToText: find("pdftotext"),
|
||||
Tesseract: find("tesseract"),
|
||||
Ghostscript: find("gs", "ghostscript"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,6 +173,39 @@ func (t Tools) DOCXToMD(docxPath, mdPath string) error {
|
||||
return t.ConvertFile(docxPath, mdPath)
|
||||
}
|
||||
|
||||
// OptimizePDF rewrites a PDF through Ghostscript (PostScript pdfwrite).
|
||||
// Preserves native text layers; strips broken OCR overlays; shrinks for OO preview.
|
||||
// Use instead of ocrmypdf when pdftotext already extracts enough text.
|
||||
func (t Tools) OptimizePDF(inPath, outPath string) error {
|
||||
if t.Ghostscript == "" {
|
||||
return fmt.Errorf("ghostscript (gs) not found on PATH")
|
||||
}
|
||||
if outPath == "" {
|
||||
return fmt.Errorf("output PDF path required")
|
||||
}
|
||||
args := []string{
|
||||
"-sDEVICE=pdfwrite",
|
||||
"-dCompatibilityLevel=1.5",
|
||||
"-dNOPAUSE", "-dQUIET", "-dBATCH",
|
||||
"-dPDFSETTINGS=/ebook",
|
||||
"-dEmbedAllFonts=true",
|
||||
"-dSubsetFonts=true",
|
||||
"-dCompressFonts=true",
|
||||
"-dCompressPages=true",
|
||||
"-dDetectDuplicateImages=true",
|
||||
"-dAutoRotatePages=/None",
|
||||
"-sOutputFile=" + outPath,
|
||||
inPath,
|
||||
}
|
||||
cmd := exec.Command(t.Ghostscript, args...)
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("ghostscript pdfwrite: %w (%s)", err, strings.TrimSpace(stderr.String()))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PDFTextLayerChars returns approximate extracted character count (0 if unavailable).
|
||||
func (t Tools) PDFTextLayerChars(pdfPath string) (int, error) {
|
||||
if t.PDFToText == "" {
|
||||
|
||||
@@ -128,6 +128,33 @@ func TestMDDocxRoundTrip(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestOptimizePDF(t *testing.T) {
|
||||
tools := LookPath()
|
||||
if tools.Ghostscript == "" || tools.PDFToText == "" {
|
||||
t.Skip("ghostscript/pdftotext not installed")
|
||||
}
|
||||
in := "/tmp/ccgg-original.pdf"
|
||||
if _, err := os.Stat(in); err != nil {
|
||||
t.Skip("local fixture not present")
|
||||
}
|
||||
dir := t.TempDir()
|
||||
out := filepath.Join(dir, "out.pdf")
|
||||
charsIn, err := tools.PDFTextLayerChars(in)
|
||||
if err != nil || charsIn < 1000 {
|
||||
t.Skip("fixture has no text layer")
|
||||
}
|
||||
if err := tools.OptimizePDF(in, out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
charsOut, err := tools.PDFTextLayerChars(out)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if charsOut < charsIn/2 {
|
||||
t.Fatalf("text layer lost: in=%d out=%d", charsIn, charsOut)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToMarkdown_PlainMD(t *testing.T) {
|
||||
tools := LookPath()
|
||||
dir := t.TempDir()
|
||||
|
||||
Reference in New Issue
Block a user