1

I would like to download pdf file by clicking on button and pdf file is opened in new tab. My pdf file URL is hidden. I cannot use pdf file URL to download it.

pdf file is on the web server. I want to download it by using puppetter in headless mode.

scrape.js

const fs = require('fs');
const puppeteer = require('puppeteer');


// set up, invoke the function, wait for the download to complete
let scrape = async () => {
    const browser = await puppeteer.launch({headless:true, ignoreHTTPSErrors: false, userDataDir: "./download", slowMo: 100}); // , dumpio: true, , executablePath: '/usr/bin/google-chrome-stable'



    const page = await browser.newPage();

    await page.goto('http://learningphp.example.com/openlink.php', {waitUntil: 'networkidle2'});


    //await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: './'})
    await page.click('body > button');
    await page.waitFor(10 * 1000);

    let result = {key: 'ok'};


    browser.close();
    return result;
};

scrape().then((value) => {
    console.log(value); // Success!
});

openlink.php

<?php
<button id="link" class="downloadLink">
    Download it!
</button>

<script type="text/javascript">
document.getElementById("link").addEventListener("click", function(){
    window.open("download.php",'_blank');
});
</script>

download.php

<?php
ob_start();
$file = "sample.pdf";

if (file_exists($file)) 
{
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit();
}

I can download pdf file if headless is false

Kana
  • 11
  • 1
  • 3
  • 3
    Possible duplicate of [How to download file with puppeteer using headless: true?](https://stackoverflow.com/questions/49245080/how-to-download-file-with-puppeteer-using-headless-true) – hardkoded Aug 01 '19 at 18:45
  • Thanks for ur answers. In this example, https://stackoverflow.com/questions/49245080/how-to-download-file-with-puppeteer-using-headless-true", downloaded file URL is known. – Kana Aug 03 '19 at 08:55

0 Answers0