Closed peterjiz closed 5 years ago
Hi @peterjiz
The following code produces the attached PDF. I did not find any issues with it. Is there something I am missing? It's not an exact match of Expectations.pdf
because I don't know what are the values of destinationWidth
, destinationHeight
, or what the ExtractPDFDimensions
function does.
I am using the development branch here but the latest release of unipdf should produce the same results.
Output: out.pdf
package main
import (
"log"
"math"
"os"
"github.com/unidoc/unipdf/creator"
"github.com/unidoc/unipdf/model"
)
func main() {
inputFilepath := "src.pdf"
outputFilepath := "out.pdf"
c := creator.New()
f, err := os.Open(inputFilepath)
if err != nil {
log.Fatal(err)
}
defer f.Close()
pdfReader, err := model.NewPdfReader(f)
if err != nil {
log.Fatal(err)
}
destinationHeight := 600.0
destinationWidth := 400.0
marginsH := 60.0
marginsV := 15.0
c.SetPageSize(creator.PageSize{destinationWidth, destinationHeight})
c.SetPageMargins(marginsH, marginsH, marginsV, marginsV)
for _, page := range pdfReader.PageList {
// Get original dimensions.
mbox := page.MediaBox
srcWidth := mbox.Urx - mbox.Llx
srcHeight := mbox.Ury - mbox.Lly
// Calculate scale.
dstWidth := srcWidth * 0.65
dstHeight := srcHeight * 0.65
minRatio := math.Min(dstHeight/srcHeight, dstWidth/srcWidth)
c.NewPage()
// Create the page block from the page.
pageBlock, err := creator.NewBlockFromPage(page)
if err != nil {
log.Fatal(err)
}
// Scale block.
pageBlock.Scale(minRatio, minRatio)
// Position block.
xDiff := (destinationWidth - pageBlock.Width() - marginsH)
yDiff := marginsV
pageBlock.SetPos(xDiff, yDiff)
// Draw
if err = c.Draw(pageBlock); err != nil {
log.Fatal(err)
}
}
if err = c.WriteToFile(outputFilepath); err != nil {
log.Fatal(err)
}
}
@adrg
I'll try the development branch some time next week, and report back on the issue!
Ended up replacing unipdf with cli calls to ghostscript and cpdf, but I would much prefer a programmatic approach: 1 portable binary with 0 external dependencies.
Edit: dimensions are 595x841.
ExtractPDFDimensions
just returns the height, width of the first page. Values are swapped if page is rotated.
@peterjiz Did you have a chance to test? Would like to close the issue if fixed.
Description
Any attempts to scale, translate, or modify the source page block, results in a damaged copy of the pdf (headers and bullet points are missing from the destination pdf)
Expected Behavior
Expectation: intact copy of the source document with scaling/translation transformation applied
Actual Behavior
Header & bullet points & possibly other elements not rendered in destination pdf.
Attachments
Code: `c := creator.New()
Workaround Code to deal with this: ` func updateMediaboxX(mediabox *pdf.PdfRectangle, xPosition, destinationWidth, blockWidth float64) (float64, float64, float64) { // x Gap is the amount of extra space between the block and the page xGap := math.Abs(math.Abs(mediabox.Urx-mediabox.Llx) - blockWidth)
}
func updateMediaboxY(mediabox *pdf.PdfRectangle, yPosition, destinationHeight, blockHeight float64) (float64, float64, float64) { // y Gap is the amount of extra space between the page and the block yGap := math.Abs(math.Abs(mediabox.Ury-mediabox.Lly) - blockHeight)
}
func pad(...){ c := creator.New()
}` Src.pdf Expectations.pdf Reality.pdf