Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporting the full page as PDF (single page) #1027

Open
jamesbuddrige opened this issue Mar 17, 2024 · 1 comment
Open

Exporting the full page as PDF (single page) #1027

jamesbuddrige opened this issue Mar 17, 2024 · 1 comment
Labels
question Questions related to rod

Comments

@jamesbuddrige
Copy link

jamesbuddrige commented Mar 17, 2024

Rod Version: v0.114.8

Is there a way for me to be able to export the page as a PDF with only a single PDF page?

So far I've tried modifying the viewport, and setting the paper height:

  • Modifying the viewport appears to make no difference to the PDF
  • Increasing the PaperHeight does increase the size of the PDF page, however, it does not increase the content (just adds white space above and below)

Example:

	page := browser.Context(ctx).MustPage()
	err := page.SetViewport(&proto.EmulationSetDeviceMetricsOverride{
		Width:  2000,
		Height: 3000,
	})
	_ = proto.EmulationSetEmulatedMedia{
		Media:    "print",
		Features: []*proto.EmulationMediaFeature{},
	}.Call(page)

	// Dynamically determine the content height
	res := page.MustEval(`() => document.body.scrollHeight`)
	if err != nil {
		return err
	}

	contentHeight := res.Num()

	// Convert the content height to inches for PDF generation (assuming 96 DPI)
	paperHeightInches := contentHeight / 96

	marginTop := 0.0
	marginBottom := 0.0
	marginLeft := 0.0
	marginRight := 0.0

	// Generate the PDF with dynamic dimensions
	reportPdf, err := page.PDF(&proto.PagePrintToPDF{
		MarginTop:       &marginTop,
		MarginBottom:    &marginBottom,
		MarginLeft:      &marginLeft,
		MarginRight:     &marginRight,
		PrintBackground: true,
		PaperHeight:     &paperHeightInches, // Convert pixels to inches for PDF generation
		PageRanges:      "1-1",              // Ensure all pages are included
		//PreferCSSPageSize: true,
	})
	if err != nil {
		return err
	}

	bin, err := afero.ReadAll(reportPdf)
	err = afero.WriteFile(fs, "report.pdf", bin, 0644)

This is something I am able to achieve in Puppeteer:

    await page.setViewport({width: 2000, height: 3000});
      const pdfOptions : PDFOptions = {
          printBackground: true,
          height: `${height} px`,
          width: `${width} px`,
          scale,
          pageRanges: "1"
      };
@jamesbuddrige jamesbuddrige added the question Questions related to rod label Mar 17, 2024
@ysmood
Copy link
Collaborator

ysmood commented Mar 18, 2024

Per the ISO 32000 standard for PDF, the page dimension limit is 14,400 PDF units in each direction. A PDF unit is 1/72 of an inch so the limit equates to a maximum page size of 200 x 200 inches. (5080 x 5080 mm).

If you have too many pages, it won't help, also the page can have page breakers, they may affect the result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions related to rod
Projects
None yet
Development

No branches or pull requests

2 participants