jung-kurt / gofpdf

A PDF document generator with high level support for text, drawing and images
http://godoc.org/github.com/jung-kurt/gofpdf
MIT License
4.33k stars 782 forks source link

Gofpdi with gofpdf import multi orientation page #317

Open ariefdfaltah opened 5 years ago

ariefdfaltah commented 5 years ago

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 commented 5 years ago

Any remarks, @phpdave11?

phpdave11 commented 5 years ago

@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 commented 5 years ago

@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 commented 4 years ago

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|-----