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 an 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 save 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 an 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 in 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 an 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 to 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 an 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 save 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 an 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 save 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 an 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 save 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 an 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 save 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 an 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 save the result to a file.
    err := client.ConvertToFile("offer.pdf")

    // Check for the conversion error.
    handleError(err)
    
    // print URL pointing to the debug log for this request.
    fmt.Println("Debug log url:", client.GetDebugLogUrl())
    
    // print Number of conversion credits remaining in your account.
    fmt.Println("Remaining credit count:", client.GetRemainingCreditCount())
    
    // print Number of credits consumed for this conversion.
    fmt.Println("Consumed credit count:", client.GetConsumedCreditCount())
    
    // print Unique identifier assigned to this conversion job.
    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 an 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 save 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 an 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 save 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())
    }
}