This page contains various examples of using the HTML to PDF API in Golang. The examples are complete and fully functional. Read more about how to convert HTML to PDF in Golang.
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Specify the mapping of HTML content width to the PDF page width. // To fine-tune the layout, you can specify an exact viewport width, such as '960px'. client.SetContentViewportWidth("balanced") // Run the conversion and save the result to a file. err := client.ConvertUrlToFile("http://www.example.com", "example.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Run the conversion and store the result in the `pdf` variable. pdf, err := client.ConvertUrl("http://www.example.com") // Check for the conversion error. handleError(err) // at this point the "pdf" variable contains PDF raw data and // can be sent in an HTTP response, saved to a file, etc. } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Create an output stream for the conversion result outputStream, err := os.Create("example.pdf") // Check for a file creation error. handleError(err) // Close the output stream. defer outputStream.Close() // run the conversion and write the result to the output stream. err = client.ConvertUrlToStream("http://www.example.com", outputStream) // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Specify the mapping of HTML content width to the PDF page width. // To fine-tune the layout, you can specify an exact viewport width, such as '960px'. client.SetContentViewportWidth("balanced") // Run the conversion and save the result to a file. err := client.ConvertFileToFile("/path/to/MyLayout.html", "MyLayout.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Run the conversion and store the result in the `pdf` variable. pdf, err := client.ConvertFile("/path/to/MyLayout.html") // Check for the conversion error. handleError(err) // at this point the "pdf" variable contains PDF raw data and // can be sent in an HTTP response, saved to a file, etc. } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Create an output stream for the conversion result outputStream, err := os.Create("MyLayout.pdf") // Check for a file creation error. handleError(err) // Close the output stream. defer outputStream.Close() // run the conversion and write the result to the output stream. err = client.ConvertFileToStream("/path/to/MyLayout.html", outputStream) // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Specify the mapping of HTML content width to the PDF page width. // To fine-tune the layout, you can specify an exact viewport width, such as '960px'. client.SetContentViewportWidth("balanced") // Run the conversion and save the result to a file. err := client.ConvertStringToFile("<html><body><h1>Hello World!</h1></body></html>", "HelloWorld.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Run the conversion and store the result in the `pdf` variable. pdf, err := client.ConvertString("<html><body><h1>Hello World!</h1></body></html>") // Check for the conversion error. handleError(err) // at this point the "pdf" variable contains PDF raw data and // can be sent in an HTTP response, saved to a file, etc. } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Create an output stream for the conversion result outputStream, err := os.Create("HelloWorld.pdf") // Check for a file creation error. handleError(err) // Close the output stream. defer outputStream.Close() // run the conversion and write the result to the output stream. err = client.ConvertStringToStream("<html><body><h1>Hello World!</h1></body></html>", outputStream) // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Specify the mapping of HTML content width to the PDF page width. // To fine-tune the layout, you can specify an exact viewport width, such as '960px'. client.SetDebugLog(true) // Run the conversion and save the result to a file. err := client.ConvertFileToFile("/path/to/MyLayout.html", "MyLayout.pdf") // Check for the conversion error. handleError(err) // print URL of the debug log fmt.Println("Debug log url:", client.GetDebugLogUrl()) // print the number of conversion credits remaining in your account fmt.Println("Remaining credit count:", client.GetRemainingCreditCount()) // print the number of credits used for the conversion fmt.Println("Consumed credit count:", client.GetConsumedCreditCount()) // print the unique identifier for the conversion fmt.Println("Job id:", client.GetJobId()) // print total number of pages in the output document fmt.Println("Page count:", client.GetPageCount()) // print size of the output data in bytes fmt.Println("Output size:", client.GetOutputSize()) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Configure the conversion. client.SetPageSize("Letter") client.SetOrientation("landscape") client.SetNoMargins(true) // Run the conversion and save the result to a file. err := client.ConvertUrlToFile("http://www.example.com", "letter_landscape.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Configure the conversion. client.SetHeaderHeight("15mm") client.SetFooterHeight("10mm") client.SetHeaderHtml("<a class='pdfcrowd-source-url' data-pdfcrowd-placement='href-and-content'></a>") client.SetFooterHtml("<center><span class='pdfcrowd-page-number'></span></center>") client.SetMarginTop("0") client.SetMarginBottom("0") // Run the conversion and save the result to a file. err := client.ConvertUrlToFile("http://www.example.com", "header_footer.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Configure the conversion. client.SetEnablePdfForms(true) // Run the conversion and save the result to a file. err := client.ConvertStringToFile("<html><body>Enter name:<input type=text></body></html>", "form.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Configure the conversion. client.SetScaleFactor(300) // Run the conversion and save the result to a file. err := client.ConvertUrlToFile("http://www.example.com", "zoom_300.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Configure the conversion. client.SetAuthor("Pdfcrowd") client.SetTitle("Hello World") client.SetSubject("Demo") client.SetKeywords("Pdfcrowd,demo") // Run the conversion and save the result to a file. err := client.ConvertUrlToFile("http://www.example.com", "with_metadata.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Configure the conversion. client.SetPageLayout("single-page") client.SetPageMode("full-screen") client.SetInitialZoomType("fit-page") client.SetOrientation("landscape") client.SetNoMargins(true) // Run the conversion and save the result to a file. err := client.ConvertUrlToFile("https://pdfcrowd.com/api/", "slide_show.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Configure the conversion. client.SetElementToConvert("#main") // Run the conversion and save the result to a file. err := client.ConvertUrlToFile("https://pdfcrowd.com/api/", "html_part.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Configure the conversion. client.SetCustomJavascript("el=document.createElement('h2'); el.textContent='Hello from Pdfcrowd API'; el.style.color='red'; el_before=document.getElementsByTagName('h1')[0]; el_before.parentNode.insertBefore(el, el_before.nextSibling)") // Run the conversion and save the result to a file. err := client.ConvertUrlToFile("http://www.example.com", "html_inject.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Configure the conversion. client.SetContentViewportWidth("large") client.SetNoMargins(true) // Run the conversion and save the result to a file. err := client.ConvertUrlToFile("https://getbootstrap.com/", "bootstrap.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" "archive/zip" "bytes" "io" "io/ioutil" ) func addFileToArchive(fName string, fPath string, writer *zip.Writer) { fStream, err := os.Open(fPath) handleError(err) var body []byte body, err = ioutil.ReadAll(fStream) handleError(err) var ioWriter io.Writer ioWriter, err = writer.Create(fName) handleError(err) _, err = ioWriter.Write(body) handleError(err) } func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Create a ZIP archive. var buffer bytes.Buffer archive := zip.NewWriter(&buffer) // Add HTML content to the archive. ioWriter, errHtml := archive.Create("index.html") handleError(errHtml) _, errHtml = ioWriter.Write([]byte(`<html> <head> <style> @font-face { font-family: 'OpenSans'; src: url(fonts/OpenSans.ttf) format('truetype'); } h1 { font-family: OpenSans; } </style> </head> <body> <h1>Hello World</h1> <img src='images/logo.png'> </body> </html>`)) handleError(errHtml) // Add required local files to the archive. addFileToArchive("fonts/OpenSans.ttf", "/your-path-to/fonts/OpenSans.ttf", archive) addFileToArchive("images/logo.png", "/your-path-to/images/logo.png", archive) handleError(archive.Flush()) handleError(archive.Close()) inStream := bytes.NewReader(buffer.Bytes()) // Create an output stream for the conversion result outputStream, err := os.Create("HelloFromZip.pdf") // Check for a file creation error. handleError(err) // Close the output stream. defer outputStream.Close() // run the conversion and write the result to the output stream. err = client.ConvertStreamToStream(inStream, outputStream) // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Configure the conversion. client.SetCustomJavascript("libPdfcrowd.highlightHtmlElements({backgroundColor: 'rgba(255, 191, 0, 0.1)', borderColor:null})") // Run the conversion and save the result to a file. err := client.ConvertUrlToFile("http://www.example.com", "highlight_background.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Configure the conversion. client.SetCustomJavascript("libPdfcrowd.highlightHtmlElements({borderColor: 'orange', backgroundColor: null, padding: '4px', margin: '4px'})") // Run the conversion and save the result to a file. err := client.ConvertUrlToFile("http://www.example.com", "highlight_borders.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Configure the conversion. client.SetDataString(`{ "name": "World", "product": "Pdfcrowd API" }`) // Run the conversion and save the result to a file. err := client.ConvertStringToFile("Hello {{ name }} from {{ product }}", "output.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Configure the conversion. client.SetDataString(`<?xml version="1.0" encoding="UTF-8"?> <data> <name>World</name> <product>Pdfcrowd API</product> </data>`) // Run the conversion and save the result to a file. err := client.ConvertStringToFile("Hello {{ data.name }} from {{ data.product }}", "output.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Configure the conversion. client.SetDataString(`name: World product: Pdfcrowd API`) // Run the conversion and save the result to a file. err := client.ConvertStringToFile("Hello {{ name }} from {{ product }}", "output.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // Configure the conversion. client.SetDataString(`name,product World,Pdfcrowd API`) // Run the conversion and save the result to a file. err := client.ConvertStringToFile("Hello {{ name }} from {{ product }}", "output.pdf") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }