Skip to content
This repository has been archived by the owner on Nov 13, 2021. It is now read-only.

Gofpdi with gofpdf import multi orientation page #317

Open
ariefdfaltah opened this issue Oct 3, 2019 · 4 comments
Open

Gofpdi with gofpdf import multi orientation page #317

ariefdfaltah opened this issue Oct 3, 2019 · 4 comments

Comments

@ariefdfaltah
Copy link

Based on this page https://github.com/jung-kurt/gofpdf/blob/master/contrib/gofpdi/gofpdi_test.go currently I have document with multiple orientation page. Legal page with portrait orientation and legal page with landscape orientation. After run code, unfortunately all of pages become legal portrait orientation. Some of content inside pages become stretch too.
Thanks!

@jung-kurt
Copy link
Owner

Any remarks, @phpdave11?

@phpdave11
Copy link
Contributor

@ariefdfaltah within the PDF specification, there is no concept of "portrait" or "landscape". Instead, you define one or more page boxes (MediaBox, TrimBox, etc.) with your desired page size.

If you want to embed multiple pages of a PDF that have different sizes, then you should call the GetPageSizes() function to determine the width and height of each page in the PDF document. You can use the returned values to scale each page while preserving the aspect ratio.

Example:

package main

import (
    "github.com/davecgh/go-spew/spew"
    fpdi "github.com/phpdave11/gofpdi"
)

func main() {
    importer := fpdi.NewImporter()
    importer.SetSourceFile("example.pdf")
    pageSizes := importer.GetPageSizes()
    spew.Dump(pageSizes)
}

Output:

(map[int]map[string]map[string]float64) (len=1) {
 (int) 1: (map[string]map[string]float64) (len=5) {
  (string) (len=9) "/MediaBox": (map[string]float64) (len=8) {
   (string) (len=3) "ury": (float64) 841.89,
   (string) (len=1) "x": (float64) 0,
   (string) (len=1) "y": (float64) 0,
   (string) (len=1) "w": (float64) 595.28,
   (string) (len=1) "h": (float64) 841.89,
   (string) (len=3) "llx": (float64) 0,
   (string) (len=3) "lly": (float64) 0,
   (string) (len=3) "urx": (float64) 595.28
  },
  (string) (len=8) "/CropBox": (map[string]float64) {
  },
  (string) (len=9) "/BleedBox": (map[string]float64) {
  },
  (string) (len=8) "/TrimBox": (map[string]float64) {
  },
  (string) (len=7) "/ArtBox": (map[string]float64) {
  }
 }
}

@ariefdfaltah
Copy link
Author

@phpdave11 the following document I use in my case example. In the OUTPUT.pdf document of each landscape page, the template is dragged and the position does not match the SOURCE.pdf document. I attach the code that I used

SOURCE.pdf

func ExampleFpdf_Import() {
	// create new pdf
	pdf := gofpdf.New("P", "pt", "A4", "")

	// I use the same function as the example on this repo
	rs, _ := getTemplatePdf()

	// create a new Importer instance
	imp := gofpdi.NewImporter()

	// import first page and determine page sizes
	tpl := imp.ImportPageFromStream(pdf, &rs, 1, "/MediaBox")
	pageSizes := imp.GetPageSizes()
	nrPages := len(imp.GetPageSizes())

	for i := 1; i <= nrPages; i++ {
		var sizeType gofpdf.SizeType
		sizeType.Wd = pageSizes[i]["/MediaBox"]["w"]
		sizeType.Ht = pageSizes[i]["/MediaBox"]["h"]
		position := PositionScanner(pageSizes[i]["/MediaBox"]["w"], pageSizes[i]["/MediaBox"]["h"])
		tpl = imp.ImportPageFromStream(pdf, &rs, i, "/MediaBox")
		pdf.AddPageFormat("P", sizeType)
		imp.UseImportedTemplate(pdf, tpl, 0, 0, pageSizes[i]["/MediaBox"]["w"], pageSizes[i]["/MediaBox"]["h"])
	}
	fileStr := "OUTPUT.pdf"
	err := pdf.OutputFileAndClose(fileStr)
}

func getTemplatePdf() (io.ReadSeeker, error) {
	file, err := os.Open("SOURCE.pdf") // For read access.
	if err != nil {
		log.Fatal(err)
	}
	readSeek := io.ReadSeeker(file)
	return readSeek, nil
}

OUTPUT.pdf

@huehnerhose
Copy link

I have the problem. My source PDF has portrait and landscape pages, when i import the source in my new pdf, the landscape pages get croped in width and get an added whitespace.

It looks like the source-page get "screenshot" with a portrait mask

-----------------
|               |
|    SOURCE     |
|               |
|               |
-----------------


|XXXXXXXXX|
|XXXXXXXXX|
|X CROP XX|
|X MASK XX|-----
|XXXXXXXXX|    |
|XXXXXXXXX|    |
|XXXXXXXXX|    |
|XXXXXXXXX|-----

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants