microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
67.23k stars 3.7k forks source link

[Feature]: Customize PDF page size in "playwright pdf" command #31057

Open liskin opened 6 months ago

liskin commented 6 months ago

🚀 Feature Request

Can we add a command-line option to playwright pdf to specify PDF page size? It seems to default to US Letter and there's no way to change it.

Example

playwright pdf --browser chromium --channel chrome --wait-for-timeout 2000 --paper-format A4 x.html x.pdf

Alternatively, simply being able to enable preferCSSPageSize might be sufficient for my usecase.

Motivation

Well… US Letter-sized PDF are a bit useless in Europe aren't they? :-)

liskin commented 6 months ago

There's this really ugly workaround if anyone else needs it:

#!/usr/bin/env node
const crPdf = require('/usr/share/nodejs/playwright-core/lib/server/chromium/crPdf.js');
const crPdf_generate = crPdf.CRPDF.prototype.generate;
crPdf.CRPDF.prototype.generate = async function (options) {
    const options2 = {
        ...options,
        preferCSSPageSize: true,
    };
    return crPdf_generate.call(this, options2);
};
require('/usr/bin/playwright')

(assumes node-playwright on a Debian-based distro)

dgozman commented 6 months ago

@liskin It is unlikely we'll add more options to the command line. However, you can just write a simple script to create a pdf and customize it to your liking.

import { chromium } from 'playwright';
const browser = await chromium.launch({ channel: 'chrome' });
const page = await browser.newPage();
await page.goto('...');
await page.waitForTimeout(2000);
await page.pdf({ path: '...', format: 'A4' });
await browser.close();

Let me know whether this helps.

liskin commented 6 months ago

Let me know whether this helps.

Well, I suppose it's a bit cleaner than monkeypatching the CLI, but I'm not sure it's actually any simpler or better.

Is there any reason why you don't want to add a --paper-format flag to the CLI? Are you trying to deprecate the CLI and instead force people to write TypeScript/JavaScript? That makes the tool quite a bit less accessible than it used to be. Having a CLI is good. Makes it possible to invoke playwright from Makefiles and shell scripts and so on.

Anyway, I'm sure we can agree that forcing the paper size to US-letter isn't exactly helpful, especially as the US is only a small part of the world population. Defaulting, sure, but forcing? Why not just set preferCSSPageSize: true if for some reason adding a command-line flag is a no-go?

dgozman commented 6 months ago

@liskin We are not going to change the defaults, e.g. set preferCSSPageSize, because that will break existing users. We are open to a pull request that adds --paper-format option.

tavro commented 2 months ago

I'd like to work on this issue @dgozman. Could it be assigned to me to ensure no one else takes it on? Thank you! :-)

hpatel292-seneca commented 1 week ago

@dgozman are you still accepting PR?