From e60be837befecbb47640318313ed6cb4648aaff7 Mon Sep 17 00:00:00 2001 From: Gustavo Efeiche Date: Wed, 9 Dec 2020 16:06:30 -0300 Subject: [PATCH] Add possibility to create new GoPdf struct from existing pdf byte array --- gopdf.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/gopdf.go b/gopdf.go index 3599b6f..2558794 100644 --- a/gopdf.go +++ b/gopdf.go @@ -74,6 +74,35 @@ type GoPdf struct { fpdi *gofpdi.Importer } +func NewFromBytes(pdfBytes []byte, config Config) (gp *GoPdf) { + gp = new(GoPdf) + gp.Start(config) + + rs := io.ReadSeeker(bytes.NewReader(pdfBytes)) + + gp.fpdi.SetSourceStream(&rs) + + templateID := gp.fpdi.ImportPage(1, "/MediaBox") + pageSizes := gp.fpdi.GetPageSizes() + + for i := 1; i < len(pageSizes); i++ { + gp.AddPage() + + if i > 1 { + templateID = gp.fpdi.ImportPage(i, "/MediaBox") + } + + gp.fpdi.UseTemplate( + templateID, + 0, + 0, + pageSizes[i]["/MediaBox"]["w"], + pageSizes[i]["/MediaBox"]["h"]) + } + + return gp +} + //SetLineWidth : set line width func (gp *GoPdf) SetLineWidth(width float64) { gp.curr.lineWidth = gp.UnitsToPoints(width)