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 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())
    }
}

Webpage to in-memory PDF

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())
    }
}

Webpage to PDF stream

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())
    }
}

HTML file to PDF file

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())
    }
}

HTML file to in-memory PDF

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())
    }
}

HTML file to PDF stream

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())
    }
}

HTML string to PDF file

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())
    }
}

HTML string to in-memory PDF

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())
    }
}

HTML string to PDF stream

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())
    }
}

Get info about the current conversion

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())
    }
}

Advanced examples

Customize the page size and the orientation

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())
    }
}

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 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())
    }
}

Create fillable PDF form

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())
    }
}

Zoom the HTML document

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())
    }
}

Set PDF metadata

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())
    }
}

Create a Powerpoint like presentation from an HTML document

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())
    }
}

Convert an HTML document section

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())
    }
}

Inject an HTML code

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())
    }
}

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 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())
    }
}

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 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())
    }
}

Renderer debugging - highlight HTML elements

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())
    }
}

Renderer debugging - borders with spacing around HTML elements

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())
    }
}

Template rendering examples

Create PDF from JSON data

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())
    }
}

Create PDF from XML data

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())
    }
}

Create PDF from YAML data

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())
    }
}

Create PDF from CSV data

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())
    }
}