PDF to PDF Golang Examples

This page contains various examples of using the PDF to PDF API in Golang. The examples are complete and fully functional. Read more about how to convert PDF to PDF in Golang.

Basic examples
PDF manipulation examples

Basic examples

Multiple PDFs to PDF file

package main

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

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

    // configure the conversion
    client.AddPdfFile("/path/to/cover.pdf")
    client.AddPdfFile("/path/to/proposal.pdf")
    client.AddPdfFile("/path/to/price.pdf")
    client.AddPdfFile("/path/to/contact.pdf")

    // run the conversion and write the result to a file
    err := client.ConvertToFile("offer.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())
    }
}

Multiple PDFs to in-memory PDF

package main

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

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

    // configure the conversion
    client.AddPdfFile("/path/to/cover.pdf")
    client.AddPdfFile("/path/to/proposal.pdf")
    client.AddPdfFile("/path/to/price.pdf")
    client.AddPdfFile("/path/to/contact.pdf")

    // run the conversion and store the result into the "pdf" variable
    pdf, err := client.Convert()

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

Multiple PDFs to PDF stream

package main

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

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

    // configure the conversion
    client.AddPdfFile("/path/to/cover.pdf")
    client.AddPdfFile("/path/to/proposal.pdf")
    client.AddPdfFile("/path/to/price.pdf")
    client.AddPdfFile("/path/to/contact.pdf")

    // create an output stream for the conversion result
    outputStream, err := os.Create("offer.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.ConvertToStream(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())
    }
}

Multiple in-memory PDFs to PDF file

package main

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

func readFile(fileName string) []byte {
    content, err := ioutil.ReadFile(fileName)
    handleError(err)
    return content
}

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

    // configure the conversion
    client.AddPdfRawData(readFile("/path/to/cover.pdf"))
    client.AddPdfRawData(readFile("/path/to/proposal.pdf"))
    client.AddPdfRawData(readFile("/path/to/price.pdf"))
    client.AddPdfRawData(readFile("/path/to/contact.pdf"))

    // run the conversion and write the result to a file
    err := client.ConvertToFile("offer.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())
    }
}

Join 2 in-memory PDFs together with 2 local PDF files to PDF file

package main

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

func readFile(fileName string) []byte {
    content, err := ioutil.ReadFile(fileName)
    handleError(err)
    return content
}

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

    // configure the conversion
    client.AddPdfRawData(readFile("/path/to/cover.pdf"))
    client.AddPdfFile("/path/to/proposal.pdf")
    client.AddPdfRawData(readFile("/path/to/price.pdf"))
    client.AddPdfFile("/path/to/contact.pdf")

    // run the conversion and write the result to a file
    err := client.ConvertToFile("offer.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())
    }
}

Watermark a PDF file

package main

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

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

    // configure the conversion
    client.AddPdfFile("/path/to/proposal.pdf")
    client.SetPageWatermark("/path/to/watermark.pdf")

    // run the conversion and write the result to a file
    err := client.ConvertToFile("company_offer.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())
    }
}

Linearize a PDF file

package main

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

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

    // configure the conversion
    client.AddPdfFile("/path/to/not_linearized.pdf")
    client.SetLinearize(true)

    // run the conversion and write the result to a file
    err := client.ConvertToFile("linearized.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())
    }
}

Get info about the current conversion

package main

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

func readFile(fileName string) []byte {
    content, err := ioutil.ReadFile(fileName)
    handleError(err)
    return content
}

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

    // configure the conversion
    client.SetDebugLog(true)
    client.AddPdfRawData(readFile("/path/to/cover.pdf"))
    client.AddPdfRawData(readFile("/path/to/proposal.pdf"))

    // run the conversion and write the result to a file
    err := client.ConvertToFile("offer.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())
    }
}

PDF manipulation examples

Extract page 3 and all pages from 7 to the end from the PDF file

package main

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

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

    // configure the conversion
    client.AddPdfFile("/your-path-to/pdfs/13_pages.pdf")
    client.SetAction("extract")
    client.SetPageRange("3,7-")

    // run the conversion and write the result to a file
    err := client.ConvertToFile("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())
    }
}

Delete the first 3 pages and the 10th page from the PDF file

package main

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

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

    // configure the conversion
    client.AddPdfFile("/your-path-to/pdfs/13_pages.pdf")
    client.SetAction("delete")
    client.SetPageRange("1-3,10")

    // run the conversion and write the result to a file
    err := client.ConvertToFile("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())
    }
}

Split the PDF file into two separate files at the 10th page

package main

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

func main() {
    client := pdfcrowd.NewPdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")
    client.AddPdfFile("/your-path-to/pdfs/13_pages.pdf")
    client.SetAction("extract")
    client.SetPageRange("1-10")
    client.ConvertToFile("pages1-10.pdf")
    client.SetPageRange("11-")
    err := client.ConvertToFile("pages11-end.pdf")
    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())
    }
}