fix: blank page when printing pdf (#43309)

This commit is contained in:
Shelley Vohr 2024-08-14 21:47:47 +02:00 committed by GitHub
parent f6bae51025
commit 286384258b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 92 additions and 17 deletions

View file

@ -2204,6 +2204,10 @@ describe('webContents module', () => {
ifdescribe(features.isPrintingEnabled())('printToPDF()', () => {
let w: BrowserWindow;
const containsText = (items: any[], text: RegExp) => {
return items.some(({ str }: { str: string }) => str.match(text));
};
beforeEach(() => {
w = new BrowserWindow({
show: false,
@ -2326,7 +2330,7 @@ describe('webContents module', () => {
});
it('with custom header and footer', async () => {
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'print-to-pdf-small.html'));
await w.loadFile(path.join(fixturesPath, 'api', 'print-to-pdf-small.html'));
const data = await w.webContents.printToPDF({
displayHeaderFooter: true,
@ -2339,11 +2343,8 @@ describe('webContents module', () => {
const { items } = await page.getTextContent();
// Check that generated PDF contains a header.
const containsText = (text: RegExp) => items.some(({ str }: { str: string }) => str.match(text));
expect(containsText(/I'm a PDF header/)).to.be.true();
expect(containsText(/I'm a PDF footer/)).to.be.true();
expect(containsText(items, /I'm a PDF header/)).to.be.true();
expect(containsText(items, /I'm a PDF footer/)).to.be.true();
});
it('in landscape mode', async () => {
@ -2395,6 +2396,25 @@ describe('webContents module', () => {
Suspects: false
});
});
it('from an existing pdf document', async () => {
const pdfPath = path.join(fixturesPath, 'cat.pdf');
await w.loadFile(pdfPath);
// TODO(codebytere): the PDF plugin is not always ready immediately
// after the document is loaded, so we need to wait for it to be ready.
// We should find a better way to do this.
await setTimeout(3000);
const data = await w.webContents.printToPDF({});
const doc = await pdfjs.getDocument(data).promise;
expect(doc.numPages).to.equal(2);
const page = await doc.getPage(1);
const { items } = await page.getTextContent();
expect(containsText(items, /Cat: The Ideal Pet/)).to.be.true();
});
});
describe('PictureInPicture video', () => {