From a10ee5edb8fbe4edf56f06e58cba360044710517 Mon Sep 17 00:00:00 2001 From: Paul Montag Date: Thu, 8 Nov 2018 13:30:29 -0600 Subject: Added page boxes --- fpdf.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'fpdf.go') diff --git a/fpdf.go b/fpdf.go index 822d87f..d735269 100644 --- a/fpdf.go +++ b/fpdf.go @@ -77,6 +77,8 @@ func fpdfNew(orientationStr, unitStr, sizeStr, fontDirStr string, size SizeType) f.pages = make([]*bytes.Buffer, 0, 8) f.pages = append(f.pages, bytes.NewBufferString("")) // pages[0] is unused (1-based) f.pageSizes = make(map[int]SizeType) + f.pageBoxes = make(map[int]map[string]PageBox) + f.defPageBoxes = make(map[string]PageBox) f.state = 0 f.fonts = make(map[string]fontDefType) f.fontFiles = make(map[string]fontFileType) @@ -335,6 +337,42 @@ func (f *Fpdf) SetCellMargin(margin float64) { f.cMargin = margin } +// SetPageBox sets the page box for the current page, and any following pages. +// Allowable types are trim, trimbox, crop, cropbox, bleed, bleedbox, art and artbox +// box types are case insensitive. +func (f *Fpdf) SetPageBox(t string, pb PageBox) { + switch strings.ToLower(t) { + case "trim": + fallthrough + case "trimbox": + t = "TrimBox" + case "crop": + fallthrough + case "cropbox": + t = "CropBox" + case "bleed": + fallthrough + case "bleedbox": + t = "BleedBox" + case "art": + fallthrough + case "artbox": + t = "ArtBox" + } + + pb.X = pb.X * f.k + pb.Y = pb.Y * f.k + pb.Wd = (pb.Wd * f.k) + pb.X + pb.Ht = (pb.Ht * f.k) + pb.Y + + if f.page > 0 { + f.pageBoxes[f.page][t] = pb + } + + // always override. page defaults are supplied in addPage function + f.defPageBoxes[t] = pb +} + // SetFontLocation sets the location in the file system of the font and font // definition files. func (f *Fpdf) SetFontLocation(fontDirStr string) { @@ -2845,6 +2883,11 @@ func (f *Fpdf) beginpage(orientationStr string, size SizeType) { return } f.page++ + // add the default page boxes, if any exist, to the page + f.pageBoxes[f.page] = make(map[string]PageBox) + for box, pb := range f.defPageBoxes { + f.pageBoxes[f.page][box] = pb + } f.pages = append(f.pages, bytes.NewBufferString("")) f.pageLinks = append(f.pageLinks, make([]linkType, 0, 0)) f.state = 2 @@ -3185,6 +3228,9 @@ func (f *Fpdf) putpages() { if ok { f.outf("/MediaBox [0 0 %.2f %.2f]", pageSize.Wd, pageSize.Ht) } + for t, pb := range f.pageBoxes[n] { + f.outf("/%s [%.2f %.2f %.2f %.2f]", t, pb.X, pb.Y, pb.Wd, pb.Ht) + } f.out("/Resources 2 0 R") // Links if len(f.pageLinks[n]) > 0 { -- cgit v1.2.1-24-ge1ad