Pass open PDFs to onepdf.Merge in the order you want them.
go get recursiveai.org/go/onepdfGo 1.24+ required (download: Go 1.26.8).
The zip includes cover.pdf, report.pdf, and appendix.pdf. If you paste the code, put those files (or your own three PDFs with those names) next to main.go. The samples say “Quarterly packet,” “Quarterly report,” and “Source notes,” respectively.
package main import ( "fmt" "log" "recursiveai.org/go/onepdf" ) func main() { names := []string{"cover.pdf", "report.pdf", "appendix.pdf"} var inputs []*onepdf.Document for _, name := range names { doc, err := onepdf.OpenFile(name) if err != nil { log.Fatal(err) } defer doc.Close() inputs = append(inputs, doc) } merged, err := onepdf.Merge(inputs) if err != nil { log.Fatal(err) } defer merged.Close() if err := merged.SaveFile("packet.pdf"); err != nil { log.Fatal(err) } fmt.Printf("packet.pdf: %d pages\n", merged.PageCount()) }
Paste the code above into main.go, run go mod init example and go get recursiveai.org/go/onepdf, then go run .; or unzip the example and go run ..
go run . prints:
packet.pdf: 3 pagesOpen packet.pdf: the cover is page 1, the report is page 2, and the appendix is page 3. Check the error from each OpenFile call before merging; a missing file stops this example before it writes a packet.
Variations
Split off the report and appendix. Replace the end of main, starting at defer merged.Close(), with the block below. Start: 1 means the second page; Count: 2 includes the page after it.
defer merged.Close() if err := merged.SaveFile("packet.pdf"); err != nil { log.Fatal(err) } span := onepdf.PageRange{Start: 1, Count: 2} parts, err := onepdf.Split(merged, span) if err != nil { log.Fatal(err) } defer parts[0].Close() if err := parts[0].SaveFile("body.pdf"); err != nil { log.Fatal(err) } fmt.Printf("packet.pdf: %d pages\n", merged.PageCount()) }
body.pdf contains “Quarterly report” followed by “Source notes.” Pass more ranges to Split to produce more documents, one per range.
Related recipes
The short answer
To merge PDFs in Go, open each file with onepdf.OpenFile, pass the documents to onepdf.Merge in the order you want them, then call SaveFile on the returned document. The merge runs locally in your Go process and leaves the input documents unchanged.
Can I concatenate PDFs in Go without a web API?
Yes. Pass a slice of open documents to Merge; it returns a new PDF 1.7 document in memory. Keep one name list as the source of truth for the order, rather than relying on the order that the files happened to arrive. For example, a two-page cover followed by a ten-page report means the report starts on output page 3. All pages of one input are copied before the next input begins.
The new file doesn’t inherit the source document information dictionary: if the packet needs a title or other document information, set it on the merged document before saving. The originals can be closed once Merge returns. Save under a new path so the original files are still available if you need to change the order and run again.
How do I split a PDF by page range in Go?
Use onepdf.Split whether the source is a freshly merged document or a PDF you opened from disk. Each PageRange is a starting index and a count, not a pair of page numbers. To get pages 3 through 5 in viewer numbering, set Start to 2 and Count to 3. The ranges may overlap or appear out of order. An empty or out-of-bounds range is an error, and the call returns no documents in that case.
The example saves the packet first, then splits a copy of the last two pages into a separate file. If your packet should contain only selected ranges from several inputs, split the inputs first and pass those returned documents to Merge instead of merging all pages and trimming afterward.