Closed denept closed 5 years ago
package main
import (
"fmt"
"os"
"github.com/jung-kurt/gofpdf/v2"
"github.com/jung-kurt/gofpdfcontrib/gofpdi"
)
func samplePDF() (fileStr string, err error) {
pdf := gofpdf.New(gofpdf.OrientationPortrait, "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "B", 16)
pdf.Cell(40, 10, "Hello World!")
fileStr = "hello.pdf"
err = pdf.OutputFileAndClose(fileStr)
return
}
func importSample(sampleFileStr string) (fileStr string, err error) {
fileStr = "sample.pdf"
pdf := gofpdf.New("P", "mm", "A4", "")
tpl1 := gofpdi.ImportPage(pdf, sampleFileStr, 1, "/MediaBox")
pdf.AddPage()
pdf.SetFillColor(200, 700, 220)
pdf.Rect(20, 50, 150, 215, "F")
gofpdi.UseImportedTemplate(pdf, tpl1, 20, 50, 150, 0)
pdf.SetFont("Helvetica", "", 20)
pdf.Cell(0, 0, "Import existing PDF into gofpdf document with gofpdi")
err = pdf.OutputFileAndClose(fileStr)
return
}
func main() {
var err error
var fileStr, sampleFileStr string
sampleFileStr, err = samplePDF()
if err == nil {
fileStr, err = importSample(sampleFileStr)
}
if err == nil {
fmt.Printf("successfully generated %s\n", fileStr)
} else {
fmt.Fprintf(os.Stderr, "error generating PDF: %s\n", err)
}
}
package main import ( "fmt" "os" "github.com/jung-kurt/gofpdf/v2" "github.com/jung-kurt/gofpdfcontrib/gofpdi" ) func samplePDF() (fileStr string, err error) { pdf := gofpdf.New(gofpdf.OrientationPortrait, "mm", "A4", "") pdf.AddPage() pdf.SetFont("Arial", "B", 16) pdf.Cell(40, 10, "Hello World!") fileStr = "hello.pdf" err = pdf.OutputFileAndClose(fileStr) return } func importSample(sampleFileStr string) (fileStr string, err error) { fileStr = "sample.pdf" pdf := gofpdf.New("P", "mm", "A4", "") tpl1 := gofpdi.ImportPage(pdf, sampleFileStr, 1, "/MediaBox") pdf.AddPage() pdf.SetFillColor(200, 700, 220) pdf.Rect(20, 50, 150, 215, "F") gofpdi.UseImportedTemplate(pdf, tpl1, 20, 50, 150, 0) pdf.SetFont("Helvetica", "", 20) pdf.Cell(0, 0, "Import existing PDF into gofpdf document with gofpdi") err = pdf.OutputFileAndClose(fileStr) return } func main() { var err error var fileStr, sampleFileStr string sampleFileStr, err = samplePDF() if err == nil { fileStr, err = importSample(sampleFileStr) } if err == nil { fmt.Printf("successfully generated %s\n", fileStr) } else { fmt.Fprintf(os.Stderr, "error generating PDF: %s\n", err) } }
感谢,但是使用后发现原文档的字体大小改变了。
Thanks, but after use, I found that the font size of the original document has changed.
Any remarks, @phpdave11? Could this be simply a problem with the coordinates passed to UseImportedTemplate()
or maybe a different font that is loaded for the original PDF?
The font size will be changed based on the width and height passed to UseImportedTemplate()
. Everything within the imported PDF will be scaled based on that as well.
If you want the font size to not change, then you need to pass in the original width and height of the imported PDF.
As a test I tried importing a PDF with the same size as the current PDF document, and I verified that the font size was the same:
package main
import (
"fmt"
"os"
"github.com/jung-kurt/gofpdf/v2"
"github.com/jung-kurt/gofpdfcontrib/gofpdi"
)
func samplePDF() (fileStr string, err error) {
pdf := gofpdf.New(gofpdf.OrientationPortrait, "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "B", 16)
pdf.Cell(40, 10, "Hello World!")
fileStr = "hello.pdf"
err = pdf.OutputFileAndClose(fileStr)
return
}
func importSample(sampleFileStr string) (fileStr string, err error) {
fileStr = "sample.pdf"
pdf := gofpdf.New("P", "mm", "A4", "")
tpl1 := gofpdi.ImportPage(pdf, sampleFileStr, 1, "/MediaBox")
pdf.AddPage()
pdf.SetFillColor(200, 700, 220)
pdf.Rect(0, 0, 210, 297, "F")
gofpdi.UseImportedTemplate(pdf, tpl1, 0, 0, 210, 297)
err = pdf.OutputFileAndClose(fileStr)
return
}
func main() {
var err error
var fileStr, sampleFileStr string
sampleFileStr, err = samplePDF()
if err == nil {
fileStr, err = importSample(sampleFileStr)
}
if err == nil {
fmt.Printf("successfully generated %s\n", fileStr)
} else {
fmt.Fprintf(os.Stderr, "error generating PDF: %s\n", err)
}
}
thank you们
请问,有没有导入pdf到模版的使用例子