gofpdi

package module
v1.0.18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2023 License: MIT Imports: 10 Imported by: 1

README

gofpdi

MIT licensed Report GoDoc

Go Free PDF Document Importer

gofpdi allows you to import an existing PDF into a new PDF. The following PDF generation libraries are supported:

Acknowledgments

This package’s code is derived from the fpdi library created by Jan Slabon. mrtsbt added support for reading a PDF from an io.ReadSeeker stream and also added support for using gofpdi concurrently. Asher Tuggle added support for reading PDFs that have split xref tables.

Examples

gopdf example
package main

import (
        "github.com/signintech/gopdf"
        "io"
        "net/http"
        "os"
)

func main() {
        var err error

        // Download a Font
        fontUrl := "https://github.com/google/fonts/raw/master/ofl/daysone/DaysOne-Regular.ttf"
        if err = DownloadFile("example-font.ttf", fontUrl); err != nil {
                panic(err)
        }

        // Download a PDF
        fileUrl := "https://tcpdf.org/files/examples/example_012.pdf"
        if err = DownloadFile("example-pdf.pdf", fileUrl); err != nil {
                panic(err)
        }

        pdf := gopdf.GoPdf{}
        pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4

        pdf.AddPage()

        err = pdf.AddTTFFont("daysone", "example-font.ttf")
        if err != nil {
                panic(err)
        }

        err = pdf.SetFont("daysone", "", 20)
        if err != nil {
                panic(err)
        }

        // Color the page
        pdf.SetLineWidth(0.1)
        pdf.SetFillColor(124, 252, 0) //setup fill color
        pdf.RectFromUpperLeftWithStyle(50, 100, 400, 600, "FD")
        pdf.SetFillColor(0, 0, 0)

        pdf.SetX(50)
        pdf.SetY(50)
        pdf.Cell(nil, "Import existing PDF into GoPDF Document")

        // Import page 1
        tpl1 := pdf.ImportPage("example-pdf.pdf", 1, "/MediaBox")

        // Draw pdf onto page
        pdf.UseImportedTemplate(tpl1, 50, 100, 400, 0)

        pdf.WritePdf("example.pdf")

}

// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func DownloadFile(filepath string, url string) error {
        // Get the data
        resp, err := http.Get(url)
        if err != nil {
                return err
        }
        defer resp.Body.Close()

        // Create the file
        out, err := os.Create(filepath)
        if err != nil {
                return err
        }
        defer out.Close()

        // Write the body to file
        _, err = io.Copy(out, resp.Body)
        return err
}

Generated PDF: example.pdf

Screenshot of PDF:

example

gofpdf example #1 - import PDF from file
package main

import (
	"github.com/phpdave11/gofpdf"
	"github.com/phpdave11/gofpdf/contrib/gofpdi"
	"io"
	"net/http"
	"os"
)

func main() {
	var err error

	pdf := gofpdf.New("P", "mm", "A4", "")

	// Download a PDF
	fileUrl := "https://tcpdf.org/files/examples/example_026.pdf"
	if err = DownloadFile("example-pdf.pdf", fileUrl); err != nil {
		panic(err)
	}

	// Import example-pdf.pdf with gofpdi free pdf document importer
	tpl1 := gofpdi.ImportPage(pdf, "example-pdf.pdf", 1, "/MediaBox")

	pdf.AddPage()

	pdf.SetFillColor(200, 700, 220)
	pdf.Rect(20, 50, 150, 215, "F")

	// Draw imported template onto page
	gofpdi.UseImportedTemplate(pdf, tpl1, 20, 50, 150, 0)

	pdf.SetFont("Helvetica", "", 20)
	pdf.Cell(0, 0, "Import existing PDF into gofpdf document with gofpdi")

	err = pdf.OutputFileAndClose("example.pdf")
	if err != nil {
		panic(err)
	}
}

// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func DownloadFile(filepath string, url string) error {
	// Get the data
	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	// Create the file
	out, err := os.Create(filepath)
	if err != nil {
		return err
	}
	defer out.Close()

	// Write the body to file
	_, err = io.Copy(out, resp.Body)
	return err
}

Generated PDF: example.pdf

Screenshot of PDF: example

gofpdf example #2 - import PDF from stream
package main

import (
    "bytes"
    "github.com/phpdave11/gofpdf"
    "github.com/phpdave11/gofpdf/contrib/gofpdi"
    "io"
    "io/ioutil"
    "net/http"
)

func main() {
    var err error

    pdf := gofpdf.New("P", "mm", "A4", "")

    // Download a PDF into memory                                                                                                                     
    res, err := http.Get("https://tcpdf.org/files/examples/example_038.pdf")
    if err != nil {
        panic(err)
    }
    pdfBytes, err := ioutil.ReadAll(res.Body)
    res.Body.Close()
    if err != nil {
        panic(err)
    }

    // convert []byte to io.ReadSeeker                                                                                                                
    rs := io.ReadSeeker(bytes.NewReader(pdfBytes))

    // Import in-memory PDF stream with gofpdi free pdf document importer                                                                             
    tpl1 := gofpdi.ImportPageFromStream(pdf, &rs, 1, "/TrimBox")

    pdf.AddPage()

    pdf.SetFillColor(200, 700, 220)
    pdf.Rect(20, 50, 150, 215, "F")

    // Draw imported template onto page                                                                                                               
    gofpdi.UseImportedTemplate(pdf, tpl1, 20, 50, 150, 0)

    pdf.SetFont("Helvetica", "", 20)
    pdf.Cell(0, 0, "Import PDF stream into gofpdf document with gofpdi")

    err = pdf.OutputFileAndClose("example.pdf")
    if err != nil {
        panic(err)
    }
}

Generated PDF:

example.pdf

Screenshot of PDF:

example.jpg

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Importer

type Importer struct {
	// contains filtered or unexported fields
}

The Importer class to be used by a pdf generation library

func NewImporter

func NewImporter() *Importer

NewImporter returns a PDF importer

func (*Importer) GetImportedObjects

func (imp *Importer) GetImportedObjects() map[int][]byte

GetImportedObjects gets object ids (int) and their contents ([]byte)

func (*Importer) GetNumPages

func (imp *Importer) GetNumPages() (int, error)

GetNumPages returns the number of pages in the PDF document

func (*Importer) GetPageSizes

func (imp *Importer) GetPageSizes() (map[int]map[string]map[string]float64, error)

GetPageSizes returns the page sizes for all pages

func (*Importer) ImportPage

func (imp *Importer) ImportPage(pageno int, box string) (int, error)

ImportPage imports a page and returns the template number

func (*Importer) PutFormXobjects

func (imp *Importer) PutFormXobjects() (map[string]int, error)

PutFormXobjects puts form xobjects and get back a map of template names (e.g. /GOFPDITPL1) and their object ids (int)

func (*Importer) SetNextObjectID

func (imp *Importer) SetNextObjectID(objID int)

SetNextObjectID sets the start object number the generated PDF code has.

func (*Importer) SetObjIDGetter added in v1.0.17

func (imp *Importer) SetObjIDGetter(f func() int)

SetObjIDGetter sets a function that is called each time the PDF writer should generate a new object number

func (*Importer) SetSourceStream

func (imp *Importer) SetSourceStream(rs io.ReadSeeker) error

SetSourceStream sets the importer source by providing a io.ReadSeeker

type PdfObject

type PdfObject struct {
	// contains filtered or unexported fields
}

type PdfObjectID

type PdfObjectID struct {
	// contains filtered or unexported fields
}

type PdfTemplate

type PdfTemplate struct {
	ID        int
	Reader    *reader.PdfReader
	Resources *reader.PdfValue
	Buffer    string
	Box       map[string]float64
	X         float64
	Y         float64
	W         float64
	H         float64
	Rotation  int
	N         int
}

Done with parsing. Now, create templates.

type PdfWriter

type PdfWriter struct {
	NextObjectID func() int
	// contains filtered or unexported fields
}

func NewPdfWriter

func NewPdfWriter() *PdfWriter

func (*PdfWriter) ClearImportedObjects

func (pw *PdfWriter) ClearImportedObjects()

ClearImportedObjects deletes all imported objects

func (*PdfWriter) GetImportedObjects

func (pw *PdfWriter) GetImportedObjects() map[*PdfObjectID][]byte

GetImportedObjects returns all byte slices for the imported objects

func (*PdfWriter) GetPDFBoxDimensions added in v1.0.18

func (pw *PdfWriter) GetPDFBoxDimensions(p int, boxname string) (map[string]float64, error)

GetPDFBoxDimensions returns the dimensions for the given box. Box must be one of "/MediaBox", "/CropBox", "/BleedBox", "/TrimBox", "/ArtBox".

func (*PdfWriter) ImportPage

func (pw *PdfWriter) ImportPage(rd *reader.PdfReader, pageno int, boxName string) (int, error)

ImportPage creates a PdfTemplate object from a page number (e.g. 1) and a boxName (e.g. /MediaBox)

func (*PdfWriter) PutFormXobjects

func (pw *PdfWriter) PutFormXobjects(reader *reader.PdfReader) (map[string]*PdfObjectID, error)

PutFormXobjects puts form xobjects and get back a map of template names (e.g. /GOFPDITPL1) and their object ids (int)

func (*PdfWriter) SetNextObjectID

func (pw *PdfWriter) SetNextObjectID(id int)

func (*PdfWriter) SetTplIDOffset

func (pw *PdfWriter) SetTplIDOffset(n int)

type TplInfo

type TplInfo struct {
	Writer     *PdfWriter
	TemplateID int
}

TplInfo has information about a template

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL