HTML to PDF Golang Examples

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.

Basic examples
Advanced examples
Template rendering examples

Basic examples

Webpage to PDF file

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // run the conversion and write 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())
    }
}

Webpage to in-memory PDF

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // run the conversion and store the result into 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())
    }
}

Webpage to PDF stream

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the 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 into 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())
    }
}

HTML file to PDF file

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // run the conversion and write 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())
    }
}

HTML file to in-memory PDF

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // run the conversion and store the result into 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())
    }
}

HTML file to PDF stream

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the 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 into 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())
    }
}

HTML string to PDF file

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // run the conversion and write 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())
    }
}

HTML string to in-memory PDF

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // run the conversion and store the result into 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())
    }
}

HTML string to PDF stream

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the 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 into 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())
    }
}

Get info about the current conversion

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetDebugLog(true)

    // run the conversion and write 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())
    }
}

Advanced examples

Customize the page size and the orientation

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetPageSize("Letter")
    client.SetOrientation("landscape")
    client.SetNoMargins(true)

    // run the conversion and write 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())
    }
}

Put the source URL in the header and the page number in the footer

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the 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 write 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())
    }
}

Create fillable PDF form

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetEnablePdfForms(true)

    // run the conversion and write 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())
    }
}

Zoom the HTML document

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetScaleFactor(300)

    // run the conversion and write 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())
    }
}

Set PDF metadata

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the 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 write 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())
    }
}

Create a Powerpoint like presentation from an HTML document

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the 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 write 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())
    }
}

Convert an HTML document section

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetElementToConvert("#main")

    // run the conversion and write 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())
    }
}

Inject an HTML code

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the 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 write 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())
    }
}

Convert a responsive web page as it appears on a large device

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetContentViewportWidth("large")
    client.SetNoMargins(true)

    // run the conversion and write 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())
    }
}

Create an in-memory archive (ZIP) and convert it

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 the API client instance
    client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // create 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 into 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())
    }
}

Renderer debugging - highlight HTML elements

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the 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 write 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())
    }
}

Renderer debugging - borders with spacing around HTML elements

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the 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 write 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())
    }
}

Template rendering examples

Create PDF from JSON data

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetDataString(`{
            "name": "World",
            "product": "Pdfcrowd API"
        }`)

    // run the conversion and write 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())
    }
}

Create PDF from XML data

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the 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 write 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())
    }
}

Create PDF from YAML data

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetDataString(`name: World
product: Pdfcrowd API`)

    // run the conversion and write 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())
    }
}

Create PDF from CSV data

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetDataString(`name,product
World,Pdfcrowd API`)

    // run the conversion and write 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())
    }
}