Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to create new GoPdf struct from existing pdf byte array #157

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions gopdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor

@akhll akhll Jan 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible that i can be less than zero ?

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)
Expand Down