Skip to content

Commit

Permalink
Merge pull request #80 from YusukeIwaki/fix_pdf_options
Browse files Browse the repository at this point in the history
fix PDF options to work
  • Loading branch information
YusukeIwaki authored Mar 26, 2021
2 parents 1883a95 + d3b5073 commit ed15d55
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 25 deletions.
21 changes: 15 additions & 6 deletions lib/puppeteer/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,16 @@ class PageError < StandardError ; end
emit_event(PageEmittedEvents::Dialog, dialog)
end

private def set_transparent_background_color(&block)
@client.send_message(
'Emulation.setDefaultBackgroundColorOverride',
color: { r: 0, g: 0, b: 0, a: 0 })
end

private def reset_default_background_color(&block)
@client.send_message('Emulation.setDefaultBackgroundColorOverride')
end

# @return [String]
def url
main_frame.url
Expand Down Expand Up @@ -949,18 +959,14 @@ def screenshot(type: nil, path: nil, full_page: nil, clip: nil, quality: nil, om
end

should_set_default_background = screenshot_options.omit_background? && format == 'png'
if should_set_default_background
@client.send_message('Emulation.setDefaultBackgroundColorOverride', color: { r: 0, g: 0, b: 0, a: 0 })
end
set_transparent_background_color if should_set_default_background
screenshot_params = {
format: format,
quality: screenshot_options.quality,
clip: clip,
}.compact
result = @client.send_message('Page.captureScreenshot', screenshot_params)
if should_set_default_background
@client.send_message('Emulation.setDefaultBackgroundColorOverride')
end
reset_default_background_color if should_set_default_background

if screenshot_options.full_page? && @viewport
self.viewport = @viewport
Expand Down Expand Up @@ -1018,7 +1024,10 @@ def initialize
# @return [String]
def pdf(options = {})
pdf_options = PDFOptions.new(options)
omit_background = options[:omit_background]
set_transparent_background_color if omit_background
result = @client.send_message('Page.printToPDF', pdf_options.page_print_args)
reset_default_background_color if omit_background
ProtocolStreamReader.new(client: @client, handle: result['stream'], path: pdf_options.path).read
rescue => err
if err.message.include?('PrintToPDF is not implemented')
Expand Down
30 changes: 15 additions & 15 deletions lib/puppeteer/page/pdf_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,24 @@ def initialize(width:, height:)
end

PAPER_FORMATS = {
letter: PaperSize.new(width: 8.5, height: 11),
legal: PaperSize.new(width: 8.5, height: 14),
tabloid: PaperSize.new(width: 11, height: 17),
ledger: PaperSize.new(width: 17, height: 11),
a0: PaperSize.new(width: 33.1, height: 46.8),
a1: PaperSize.new(width: 23.4, height: 33.1),
a2: PaperSize.new(width: 16.54, height: 23.4),
a3: PaperSize.new(width: 11.7, height: 16.54),
a4: PaperSize.new(width: 8.27, height: 11.7),
a5: PaperSize.new(width: 5.83, height: 8.27),
a6: PaperSize.new(width: 4.13, height: 5.83),
'letter' => PaperSize.new(width: 8.5, height: 11),
'legal' => PaperSize.new(width: 8.5, height: 14),
'tabloid' => PaperSize.new(width: 11, height: 17),
'ledger' => PaperSize.new(width: 17, height: 11),
'a0' => PaperSize.new(width: 33.1, height: 46.8),
'a1' => PaperSize.new(width: 23.4, height: 33.1),
'a2' => PaperSize.new(width: 16.54, height: 23.4),
'a3' => PaperSize.new(width: 11.7, height: 16.54),
'a4' => PaperSize.new(width: 8.27, height: 11.7),
'a5' => PaperSize.new(width: 5.83, height: 8.27),
'a6' => PaperSize.new(width: 4.13, height: 5.83),
}

UNIT_TO_PIXELS = {
px: 1,
in: 96,
cm: 37.8,
mm: 3.78,
'px' => 1,
'in' => 96,
'cm' => 37.8,
'mm' => 3.78,
}

# @param parameter [String|Integer|nil]
Expand Down
33 changes: 29 additions & 4 deletions spec/integration/example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,35 @@
end
end

it {
page.goto('https://twitter.com/AndroidDev')
Timeout.timeout(10) { page.wait_for_selector('article[role="article"]') }
}
it 'should print PDF with options' do
skip if Puppeteer.env.ci? && !Puppeteer.env.windows?

page.viewport = Puppeteer::Viewport.new(width: 1200, height: 800, device_scale_factor: 2)
page.goto("https://github.com/YusukeIwaki")
page.wait_for_selector(".js-yearly-contributions")
overlay = page.query_selector('.js-yearly-contributions')

js = <<-JAVASCRIPT
graph => {
const width = getComputedStyle(graph).width;
graph = graph.cloneNode(true);
graph.style.width = width;
document.body.innerHTML = `
<div style="display:flex;justify-content:center;align-items:center;height:100vh;">;
${graph.outerHTML}
</div>
`;
}
JAVASCRIPT
page.evaluate(js, overlay)
page.pdf(
path: '5.element-to-pdf.github.pdf',
print_background: true,
format: "letter",
margin: { top: "1cm", left: "2cm", right: "3cm", bottom: "4cm" },
omit_background: true,
)
end

it 'should input text and grab DOM elements' do
skip if Puppeteer.env.ci? && !Puppeteer.env.windows?
Expand Down

0 comments on commit ed15d55

Please sign in to comment.