diff options
author | Kurt <kurt.w.jung@gmail.com> | 2016-02-18 15:48:47 -0500 |
---|---|---|
committer | Kurt <kurt.w.jung@gmail.com> | 2016-02-18 15:48:47 -0500 |
commit | b56ba6c31fb6e12aab4bc92b7f823f2b3acb5742 (patch) | |
tree | d66265ba55ad477643701a7476dcc69ed9f8bd7b | |
parent | 0d15f36c67d7e7494aa0eb5a1d94cd766835c257 (diff) |
Demonstration of PDF size reduction with ghostscript
-rw-r--r-- | contrib/ghostscript/ghostscript.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/contrib/ghostscript/ghostscript.go b/contrib/ghostscript/ghostscript.go new file mode 100644 index 0000000..90f9135 --- /dev/null +++ b/contrib/ghostscript/ghostscript.go @@ -0,0 +1,62 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + + "github.com/jung-kurt/gofpdf" +) + +func report(fileStr string, err error) { + if err == nil { + var info os.FileInfo + info, err = os.Stat(fileStr) + if err == nil { + fmt.Printf("%s: OK, size %d\n", fileStr, info.Size()) + } else { + fmt.Printf("%s: bad stat\n", fileStr) + } + } else { + fmt.Printf("%s: %s\n", fileStr, err) + } +} + +func newPdf() (pdf *gofpdf.Fpdf) { + pdf = gofpdf.New("P", "mm", "A4", "../../font") + pdf.SetCompression(false) + pdf.AddFont("Calligrapher", "", "calligra.json") + pdf.AddPage() + pdf.SetFont("Calligrapher", "", 35) + pdf.Cell(0, 10, "Enjoy new fonts with FPDF!") + return +} + +func full() { + const name = "full.pdf" + report(name, newPdf().OutputFileAndClose(name)) +} + +func min() { + const name = "min.pdf" + cmd := exec.Command("gs", "-sDEVICE=pdfwrite", + "-dCompatibilityLevel=1.4", + "-dPDFSETTINGS=/screen", "-dNOPAUSE", "-dQUIET", + "-dBATCH", "-sOutputFile="+name, "-") + inPipe, err := cmd.StdinPipe() + if err == nil { + errChan := make(chan error, 1) + go func() { + errChan <- cmd.Start() + }() + newPdf().Output(inPipe) + report(name, <-errChan) + } else { + report(name, err) + } +} + +func main() { + full() + min() +} |