github.com/phpdave11/gofpdf@v1.4.2/contrib/gofpdi/gofpdi.go (about)

     1  /*
     2  Package gofpdi wraps the gofpdi PDF library to import existing PDFs as templates. See github.com/phpdave11/gofpdi
     3  for further information and examples.
     4  
     5  Users should call NewImporter() to obtain their own Importer instance to work with.
     6  To retain backwards compatibility, the package offers a default Importer that may be used via global functions. Note
     7  however that use of the default Importer is not thread safe.
     8  */
     9  package gofpdi
    10  
    11  import (
    12  	realgofpdi "github.com/phpdave11/gofpdi"
    13  	"io"
    14  )
    15  
    16  // gofpdiPdf is a partial interface that only implements the functions we need
    17  // from the PDF generator to put the imported PDF templates on the PDF.
    18  type gofpdiPdf interface {
    19  	ImportObjects(objs map[string][]byte)
    20  	ImportObjPos(objs map[string]map[int]string)
    21  	ImportTemplates(tpls map[string]string)
    22  	UseImportedTemplate(tplName string, x float64, y float64, w float64, h float64)
    23  	SetError(err error)
    24  }
    25  
    26  // Importer wraps an Importer from the gofpdi library.
    27  type Importer struct {
    28  	fpdi *realgofpdi.Importer
    29  }
    30  
    31  // NewImporter creates a new Importer wrapping functionality from the gofpdi library.
    32  func NewImporter() *Importer {
    33  	return &Importer{
    34  		fpdi: realgofpdi.NewImporter(),
    35  	}
    36  }
    37  
    38  // ImportPage imports a page of a PDF file with the specified box (/MediaBox,
    39  // /TrimBox, /ArtBox, /CropBox, or /BleedBox). Returns a template id that can
    40  // be used with UseImportedTemplate to draw the template onto the page.
    41  func (i *Importer) ImportPage(f gofpdiPdf, sourceFile string, pageno int, box string) int {
    42  	// Set source file for fpdi
    43  	i.fpdi.SetSourceFile(sourceFile)
    44  	// return template id
    45  	return i.getTemplateID(f, pageno, box)
    46  }
    47  
    48  // ImportPageFromStream imports a page of a PDF with the specified box
    49  // (/MediaBox, TrimBox, /ArtBox, /CropBox, or /BleedBox). Returns a template id
    50  // that can be used with UseImportedTemplate to draw the template onto the
    51  // page.
    52  func (i *Importer) ImportPageFromStream(f gofpdiPdf, rs *io.ReadSeeker, pageno int, box string) int {
    53  	// Set source stream for fpdi
    54  	i.fpdi.SetSourceStream(rs)
    55  	// return template id
    56  	return i.getTemplateID(f, pageno, box)
    57  }
    58  
    59  func (i *Importer) getTemplateID(f gofpdiPdf, pageno int, box string) int {
    60  	// Import page
    61  	tpl := i.fpdi.ImportPage(pageno, box)
    62  
    63  	// Import objects into current pdf document
    64  	// Unordered means that the objects will be returned with a sha1 hash instead of an integer
    65  	// The objects themselves may have references to other hashes which will be replaced in ImportObjects()
    66  	tplObjIDs := i.fpdi.PutFormXobjectsUnordered()
    67  
    68  	// Set template names and ids (hashes) in gofpdf
    69  	f.ImportTemplates(tplObjIDs)
    70  
    71  	// Get a map[string]string of the imported objects.
    72  	// The map keys will be the ID of each object.
    73  	imported := i.fpdi.GetImportedObjectsUnordered()
    74  
    75  	// Import gofpdi objects into gofpdf
    76  	f.ImportObjects(imported)
    77  
    78  	// Get a map[string]map[int]string of the object hashes and their positions within each object,
    79  	// to be replaced with object ids (integers).
    80  	importedObjPos := i.fpdi.GetImportedObjHashPos()
    81  
    82  	// Import gofpdi object hashes and their positions into gopdf
    83  	f.ImportObjPos(importedObjPos)
    84  
    85  	return tpl
    86  }
    87  
    88  // UseImportedTemplate draws the template onto the page at x,y. If w is 0, the
    89  // template will be scaled to fit based on h. If h is 0, the template will be
    90  // scaled to fit based on w.
    91  func (i *Importer) UseImportedTemplate(f gofpdiPdf, tplid int, x float64, y float64, w float64, h float64) {
    92  	// Get values from fpdi
    93  	tplName, scaleX, scaleY, tX, tY := i.fpdi.UseTemplate(tplid, x, y, w, h)
    94  
    95  	f.UseImportedTemplate(tplName, scaleX, scaleY, tX, tY)
    96  }
    97  
    98  // GetPageSizes returns page dimensions for all pages of the imported pdf.
    99  // Result consists of map[<page number>]map[<box>]map[<dimension>]<value>.
   100  // <page number>: page number, note that page numbers start at 1
   101  // <box>: box identifier, e.g. "/MediaBox"
   102  // <dimension>: dimension string, either "w" or "h"
   103  func (i *Importer) GetPageSizes() map[int]map[string]map[string]float64 {
   104  	return i.fpdi.GetPageSizes()
   105  }
   106  
   107  // Default Importer used by global functions
   108  var fpdi = NewImporter()
   109  
   110  // ImportPage imports a page of a PDF file with the specified box (/MediaBox,
   111  // /TrimBox, /ArtBox, /CropBox, or /BleedBox). Returns a template id that can
   112  // be used with UseImportedTemplate to draw the template onto the page.
   113  // Note: This uses the default Importer. Call NewImporter() to obtain a custom Importer.
   114  func ImportPage(f gofpdiPdf, sourceFile string, pageno int, box string) int {
   115  	return fpdi.ImportPage(f, sourceFile, pageno, box)
   116  }
   117  
   118  // ImportPageFromStream imports a page of a PDF with the specified box
   119  // (/MediaBox, TrimBox, /ArtBox, /CropBox, or /BleedBox). Returns a template id
   120  // that can be used with UseImportedTemplate to draw the template onto the
   121  // page.
   122  // Note: This uses the default Importer. Call NewImporter() to obtain a custom Importer.
   123  func ImportPageFromStream(f gofpdiPdf, rs *io.ReadSeeker, pageno int, box string) int {
   124  	return fpdi.ImportPageFromStream(f, rs, pageno, box)
   125  }
   126  
   127  // UseImportedTemplate draws the template onto the page at x,y. If w is 0, the
   128  // template will be scaled to fit based on h. If h is 0, the template will be
   129  // scaled to fit based on w.
   130  // Note: This uses the default Importer. Call NewImporter() to obtain a custom Importer.
   131  func UseImportedTemplate(f gofpdiPdf, tplid int, x float64, y float64, w float64, h float64) {
   132  	fpdi.UseImportedTemplate(f, tplid, x, y, w, h)
   133  }
   134  
   135  // GetPageSizes returns page dimensions for all pages of the imported pdf.
   136  // Result consists of map[<page number>]map[<box>]map[<dimension>]<value>.
   137  // <page number>: page number, note that page numbers start at 1
   138  // <box>: box identifier, e.g. "/MediaBox"
   139  // <dimension>: dimension string, either "w" or "h"
   140  // Note: This uses the default Importer. Call NewImporter() to obtain a custom Importer.
   141  func GetPageSizes() map[int]map[string]map[string]float64 {
   142  	return fpdi.GetPageSizes()
   143  }