mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-17 04:32:15 +01:00
feat(chromium): single page PDF
This commit is contained in:
@@ -950,6 +950,41 @@ func TestChromiumBrowser_pdf(t *testing.T) {
|
|||||||
"wait until 'window.globalVar === 'ready'' is true before print",
|
"wait until 'window.globalVar === 'ready'' is true before print",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
scenario: "single page",
|
||||||
|
browser: newChromiumBrowser(
|
||||||
|
browserArguments{
|
||||||
|
binPath: os.Getenv("CHROMIUM_BIN_PATH"),
|
||||||
|
wsUrlReadTimeout: 5 * time.Second,
|
||||||
|
allowList: regexp2.MustCompile("", 0),
|
||||||
|
denyList: regexp2.MustCompile("", 0),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
fs: func() *gotenberg.FileSystem {
|
||||||
|
fs := gotenberg.NewFileSystem()
|
||||||
|
|
||||||
|
err := os.MkdirAll(fs.WorkingDirPath(), 0o755)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf(fmt.Sprintf("expected no error but got: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile(fmt.Sprintf("%s/index.html", fs.WorkingDirPath()), []byte("<h1>Custom header and footer</h1>"), 0o755)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected no error but got: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fs
|
||||||
|
}(),
|
||||||
|
options: PdfOptions{
|
||||||
|
SinglePage: true,
|
||||||
|
},
|
||||||
|
noDeadline: false,
|
||||||
|
start: true,
|
||||||
|
expectError: false,
|
||||||
|
expectedLogEntries: []string{
|
||||||
|
"single page PDF",
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
scenario: "custom header and footer",
|
scenario: "custom header and footer",
|
||||||
browser: newChromiumBrowser(
|
browser: newChromiumBrowser(
|
||||||
|
|||||||
@@ -150,6 +150,11 @@ type PdfOptions struct {
|
|||||||
// Optional.
|
// Optional.
|
||||||
Scale float64
|
Scale float64
|
||||||
|
|
||||||
|
// SinglePage defines whether to print the entire content in one single
|
||||||
|
// page.
|
||||||
|
// Optional.
|
||||||
|
SinglePage bool
|
||||||
|
|
||||||
// PaperWidth is the paper width, in inches.
|
// PaperWidth is the paper width, in inches.
|
||||||
// Optional.
|
// Optional.
|
||||||
PaperWidth float64
|
PaperWidth float64
|
||||||
@@ -209,6 +214,7 @@ func DefaultPdfOptions() PdfOptions {
|
|||||||
Landscape: false,
|
Landscape: false,
|
||||||
PrintBackground: false,
|
PrintBackground: false,
|
||||||
Scale: 1.0,
|
Scale: 1.0,
|
||||||
|
SinglePage: false,
|
||||||
PaperWidth: 8.5,
|
PaperWidth: 8.5,
|
||||||
PaperHeight: 11,
|
PaperHeight: 11,
|
||||||
MarginTop: 0.39,
|
MarginTop: 0.39,
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ func FormDataChromiumPdfOptions(ctx *api.Context) (*api.FormData, PdfOptions) {
|
|||||||
defaultPdfOptions := DefaultPdfOptions()
|
defaultPdfOptions := DefaultPdfOptions()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
landscape, printBackground bool
|
landscape, printBackground, singlePage bool
|
||||||
scale, paperWidth, paperHeight float64
|
scale, paperWidth, paperHeight float64
|
||||||
marginTop, marginBottom, marginLeft, marginRight float64
|
marginTop, marginBottom, marginLeft, marginRight float64
|
||||||
pageRanges string
|
pageRanges string
|
||||||
@@ -121,6 +121,7 @@ func FormDataChromiumPdfOptions(ctx *api.Context) (*api.FormData, PdfOptions) {
|
|||||||
Bool("landscape", &landscape, defaultPdfOptions.Landscape).
|
Bool("landscape", &landscape, defaultPdfOptions.Landscape).
|
||||||
Bool("printBackground", &printBackground, defaultPdfOptions.PrintBackground).
|
Bool("printBackground", &printBackground, defaultPdfOptions.PrintBackground).
|
||||||
Float64("scale", &scale, defaultPdfOptions.Scale).
|
Float64("scale", &scale, defaultPdfOptions.Scale).
|
||||||
|
Bool("singlePage", &singlePage, defaultPdfOptions.SinglePage).
|
||||||
Float64("paperWidth", &paperWidth, defaultPdfOptions.PaperWidth).
|
Float64("paperWidth", &paperWidth, defaultPdfOptions.PaperWidth).
|
||||||
Float64("paperHeight", &paperHeight, defaultPdfOptions.PaperHeight).
|
Float64("paperHeight", &paperHeight, defaultPdfOptions.PaperHeight).
|
||||||
Float64("marginTop", &marginTop, defaultPdfOptions.MarginTop).
|
Float64("marginTop", &marginTop, defaultPdfOptions.MarginTop).
|
||||||
@@ -137,6 +138,7 @@ func FormDataChromiumPdfOptions(ctx *api.Context) (*api.FormData, PdfOptions) {
|
|||||||
Landscape: landscape,
|
Landscape: landscape,
|
||||||
PrintBackground: printBackground,
|
PrintBackground: printBackground,
|
||||||
Scale: scale,
|
Scale: scale,
|
||||||
|
SinglePage: singlePage,
|
||||||
PaperWidth: paperWidth,
|
PaperWidth: paperWidth,
|
||||||
PaperHeight: paperHeight,
|
PaperHeight: paperHeight,
|
||||||
MarginTop: marginTop,
|
MarginTop: marginTop,
|
||||||
|
|||||||
@@ -17,19 +17,37 @@ import (
|
|||||||
|
|
||||||
func printToPdfActionFunc(logger *zap.Logger, outputPath string, options PdfOptions) chromedp.ActionFunc {
|
func printToPdfActionFunc(logger *zap.Logger, outputPath string, options PdfOptions) chromedp.ActionFunc {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
|
paperHeight := options.PaperHeight
|
||||||
|
pageRanges := options.PageRanges
|
||||||
|
|
||||||
|
if options.SinglePage {
|
||||||
|
logger.Debug("single page PDF")
|
||||||
|
|
||||||
|
_, _, _, _, _, cssContentSize, err := page.GetLayoutMetrics().Do(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("get layout metrics: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// There are 96 CSS pixels per inch.
|
||||||
|
// See https://issues.chromium.org/issues/40267771#comment14.
|
||||||
|
paperHeight = cssContentSize.Height / 96
|
||||||
|
pageRanges = "1" // little dirty hack to avoid leftovers.
|
||||||
|
}
|
||||||
|
|
||||||
printToPdf := page.PrintToPDF().
|
printToPdf := page.PrintToPDF().
|
||||||
WithTransferMode(page.PrintToPDFTransferModeReturnAsStream).
|
WithTransferMode(page.PrintToPDFTransferModeReturnAsStream).
|
||||||
WithLandscape(options.Landscape).
|
WithLandscape(options.Landscape).
|
||||||
WithPrintBackground(options.PrintBackground).
|
WithPrintBackground(options.PrintBackground).
|
||||||
WithScale(options.Scale).
|
WithScale(options.Scale).
|
||||||
WithPaperWidth(options.PaperWidth).
|
WithPaperWidth(options.PaperWidth).
|
||||||
WithPaperHeight(options.PaperHeight).
|
WithPaperHeight(paperHeight).
|
||||||
WithMarginTop(options.MarginTop).
|
WithMarginTop(options.MarginTop).
|
||||||
WithMarginBottom(options.MarginBottom).
|
WithMarginBottom(options.MarginBottom).
|
||||||
WithMarginLeft(options.MarginLeft).
|
WithMarginLeft(options.MarginLeft).
|
||||||
WithMarginRight(options.MarginRight).
|
WithMarginRight(options.MarginRight).
|
||||||
WithPageRanges(options.PageRanges).
|
WithPageRanges(pageRanges).
|
||||||
WithPreferCSSPageSize(options.PreferCssPageSize)
|
WithPreferCSSPageSize(options.PreferCssPageSize).
|
||||||
|
WithGenerateTaggedPDF(true)
|
||||||
|
|
||||||
hasCustomHeaderFooter := options.HeaderTemplate != DefaultPdfOptions().HeaderTemplate ||
|
hasCustomHeaderFooter := options.HeaderTemplate != DefaultPdfOptions().HeaderTemplate ||
|
||||||
options.FooterTemplate != DefaultPdfOptions().FooterTemplate
|
options.FooterTemplate != DefaultPdfOptions().FooterTemplate
|
||||||
|
|||||||
Reference in New Issue
Block a user