github.com/phpdave11/gofpdf@v1.4.2/contrib/ghostscript/ghostscript.go (about) 1 package main 2 3 // This command demonstrates the use of ghotscript to reduce the size 4 // of generated PDFs. This is based on a comment made by farkerhaiku: 5 // https://github.com/phpdave11/gofpdf/issues/57#issuecomment-185843315 6 7 import ( 8 "fmt" 9 "os" 10 "os/exec" 11 12 "github.com/phpdave11/gofpdf" 13 ) 14 15 func report(fileStr string, err error) { 16 if err == nil { 17 var info os.FileInfo 18 info, err = os.Stat(fileStr) 19 if err == nil { 20 fmt.Printf("%s: OK, size %d\n", fileStr, info.Size()) 21 } else { 22 fmt.Printf("%s: bad stat\n", fileStr) 23 } 24 } else { 25 fmt.Printf("%s: %s\n", fileStr, err) 26 } 27 } 28 29 func newPdf() (pdf *gofpdf.Fpdf) { 30 pdf = gofpdf.New("P", "mm", "A4", "../../font") 31 pdf.SetCompression(false) 32 pdf.AddFont("Calligrapher", "", "calligra.json") 33 pdf.AddPage() 34 pdf.SetFont("Calligrapher", "", 35) 35 pdf.Cell(0, 10, "Enjoy new fonts with FPDF!") 36 return 37 } 38 39 func full(name string) { 40 report(name, newPdf().OutputFileAndClose(name)) 41 } 42 43 func min(name string) { 44 cmd := exec.Command("gs", "-sDEVICE=pdfwrite", 45 "-dCompatibilityLevel=1.4", 46 "-dPDFSETTINGS=/screen", "-dNOPAUSE", "-dQUIET", 47 "-dBATCH", "-sOutputFile="+name, "-") 48 inPipe, err := cmd.StdinPipe() 49 if err == nil { 50 errChan := make(chan error, 1) 51 go func() { 52 errChan <- cmd.Start() 53 }() 54 err = newPdf().Output(inPipe) 55 if err == nil { 56 report(name, <-errChan) 57 } else { 58 report(name, err) 59 } 60 } else { 61 report(name, err) 62 } 63 } 64 65 func main() { 66 full("full.pdf") 67 min("min.pdf") 68 }