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

Import all pages from existing pdf file, Fix table border corners and Set current page method #310

Merged
merged 5 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: Import pages source can be a file path, byte slice, or (*)io.Re…
…adSeeker
  • Loading branch information
liasica committed Oct 17, 2024
commit 85230af197158ec017965c2a33106f52956dec12
70 changes: 69 additions & 1 deletion gopdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -1574,7 +1574,6 @@ func (gp *GoPdf) UseImportedTemplate(tplid int, x float64, y float64, w float64,
gp.getContent().AppendStreamImportedTemplate(tplName, scaleX, scaleY, tX, tY)
}


// ImportPagesFromFile imports pages from a source file.
func (gp *GoPdf) ImportPagesFromFile(sourceFile string, box string) error {
// Set source file for fpdi
Expand Down Expand Up @@ -1628,6 +1627,75 @@ func (gp *GoPdf) ImportPagesFromFile(sourceFile string, box string) error {
return nil
}

// ImportPagesFromSource imports pages from a source pdf.
// The source can be a file path, byte slice, or (*)io.ReadSeeker.
func (gp *GoPdf) ImportPagesFromSource(source interface{}, box string) error {
switch v := source.(type) {
case string:
// Set source file for fpdi
gp.fpdi.SetSourceFile(v)
case []byte:
// Set source stream for fpdi
rs := io.ReadSeeker(bytes.NewReader(v))
gp.fpdi.SetSourceStream(&rs)
case io.ReadSeeker:
// Set source stream for fpdi
gp.fpdi.SetSourceStream(&v)
case *io.ReadSeeker:
// Set source stream for fpdi
gp.fpdi.SetSourceStream(v)
default:
return errors.New("source type not supported")
}

// Get number of pages from source file
pages := gp.fpdi.GetNumPages()

// Get page sizes from source file
sizes := gp.fpdi.GetPageSizes()

for i := 0; i < pages; i++ {
pageno := i + 1

// Get the size of the page
size, ok := sizes[pageno][box]
if !ok {
return errors.New("can not get page size")
}

// Add a new page to the document
gp.AddPage()

// gofpdi needs to know where to start the object id at.
// By default, it starts at 1, but gopdf adds a few objects initially.
startObjID := gp.GetNextObjectID()

// Set gofpdi next object ID to whatever the value of startObjID is
gp.fpdi.SetNextObjectID(startObjID)

// Import page
tpl := gp.fpdi.ImportPage(pageno, box)

// Import objects into current pdf document
tplObjIDs := gp.fpdi.PutFormXobjects()

// Set template names and ids in gopdf
gp.ImportTemplates(tplObjIDs)

// Get a map[int]string of the imported objects.
// The map keys will be the ID of each object.
imported := gp.fpdi.GetImportedObjects()

// Import gofpdi objects into gopdf, starting at whatever the value of startObjID is
gp.ImportObjects(imported, startObjID)

// Draws the imported template on the current page
gp.UseImportedTemplate(tpl, 0, 0, size["w"], size["h"])
}

return nil
}

// GetNextObjectID gets the next object ID so that gofpdi knows where to start the object IDs.
func (gp *GoPdf) GetNextObjectID() int {
return len(gp.pdfObjs) + 1
Expand Down
2 changes: 1 addition & 1 deletion gopdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ func TestImportPagesFromFile(t *testing.T) {
pdf := GoPdf{}
pdf.Start(Config{PageSize: *PageSizeA4})

err = pdf.ImportPagesFromFile("./examples/outline_example/outline_demo.pdf", "/MediaBox")
err = pdf.ImportPagesFromSource("./examples/outline_example/outline_demo.pdf", "/MediaBox")
if err != nil {
t.Error(err)
return
Expand Down