Last active
April 5, 2018 05:42
-
-
Save j4cksw/daba61a9fd0a2f2da924a05d1c0ae50b to your computer and use it in GitHub Desktop.
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
| package main | |
| /* | |
| Required imagemagick | |
| $brew install imagemagick | |
| And Ghostscript | |
| $brew install ghostscript | |
| */ | |
| import ( | |
| "fmt" | |
| "github.com/jung-kurt/gofpdf" | |
| "gopkg.in/gographics/imagick.v3/imagick" | |
| ) | |
| func main() { | |
| Example() | |
| } | |
| const PDF_FILENAME = "hello.pdf" | |
| func Example() { | |
| genPDF(PDF_FILENAME) | |
| genImageFromPDF(PDF_FILENAME) | |
| } | |
| func genPDF(pdfFilename string) { | |
| pdf := gofpdf.New("P", "mm", "A4", "") | |
| pdf.AddPage() | |
| pdf.SetFont("Arial", "B", 16) | |
| pdf.Cell(40, 10, "Hello, world") | |
| err := pdf.OutputFileAndClose(pdfFilename) | |
| if err != nil { | |
| fmt.Println(err) | |
| } | |
| } | |
| func genImageFromPDF(pdfFileName string) { | |
| imagick.Initialize() | |
| defer imagick.Terminate() | |
| mw := imagick.NewMagickWand() | |
| defer func() { | |
| mw.Clear() | |
| mw.Destroy() | |
| }() | |
| pw := imagick.NewPixelWand() | |
| defer func() { | |
| pw.Clear() | |
| pw.Destroy() | |
| }() | |
| bg := imagick.NewMagickWand() | |
| defer func() { | |
| bg.Clear() | |
| bg.Destroy() | |
| }() | |
| readErr := mw.ReadImage("hello.pdf") | |
| if readErr != nil { | |
| fmt.Println(readErr) | |
| } | |
| x, y, _ := mw.GetImageResolution() | |
| bg.SetResolution(x, y) | |
| pw.SetColor("white") | |
| bg.NewImage(mw.GetImageWidth(), mw.GetImageHeight(), pw) | |
| bg.CompositeImage(mw, imagick.COMPOSITE_OP_OVER, false, 0, 0) | |
| bg.SetImageFormat("jpg") | |
| bg.ResizeImage(1040, 1040, imagick.FILTER_BOX) | |
| writeErr := bg.WriteImage("hello.jpg") | |
| if writeErr != nil { | |
| fmt.Println(writeErr) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment