A
A
Alexander2020-09-04 11:05:26
JavaScript
Alexander, 2020-09-04 11:05:26

How to save pdf page with puppeteer after redirect?

Greetings dear community.
This is my first experience with puppeteer and similar software, so don't judge too harshly.
I'm trying to download a PDF and I don't understand how to complete it
. I have a link that causes a redirect to the PDF generation service
. ready PDF but then the browser stops and that's it - saving does not occur and the browser does not close browser.close(); does not work
What's the joke? I tried page.waitForNavigation(), also to the point

(async () => {
    const cookiesString = await fs.readFile(path.join(__dirname, '/../tmp/cookies.json'));
    const cookies = JSON.parse(cookiesString);

    const browser = await puppeteer.launch({
        headless: false,
    });
    const page = await browser.newPage();
    await page.setCookie(...cookies);

    await page.goto(url);
    console.log(page.url())
    // url = https://www.copart.com/invoice/pdf?invoiceNumber=*******&paymentType=U&currencyCode=USD....'

    // происходит редирект на сторонний сервис который генерит ПДФ, 
    // например как ссылка в комменте ниже, по ней отдается уже готовый ПДФ
    // https://services.copart.com/pdf-ws/pdf/********?st=JKQXU5UXNvf-MrzVKANNvg&e=15997

    // теперь я пытаюсь сохранить полученый результат в файл
    const file = await page.pdf({
        path: path.join(__dirname, '/../invoices'),
        format: 'A4'
    });

    await browser.close();
})();


I looked at the headlines
const response = await page.goto(url);
    console.log(response.headers())

returns 'content-type': 'application/pdf'

page.pdf().catch(e=>console.log(e)); returns an error
Error: Protocol error (Page.printToPDF): PrintToPDF is not implemented at Promise (/home/alex/www/driver/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:208:63) at new Promise ( ) at CDPSession.send (/home/alex/www/driver/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:207:16) at Page.pdf (/home/alex/www/driver/node_modules /puppeteer/lib/cjs/puppeteer/common/Page.js:1152:43) at /home/alex/www/driver/bin/downloadInvoice.js:25:29 at process._tickCallback (internal/process/next_tick.js :68:7) message: 'Protocol error (Page.printToPDF): PrintToPDF is not implemented'

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question