简体   繁体   中英

how can i save rendered pdf (generated from html) to my computer?

I am using below API to generate pdf from URL:

https://url-to-pdf-api.herokuapp.com/api/render?url=http://google.com

I want a php code which directly downloads the generated pdf. below is what I have tried:

a href="https://url-to-pdf-api.herokuapp.com/api/render?url=http://google.com" download="download"

but, the file does not download!

the expected output should be a pdf file downloaded.

you can use

<form method="get" action="https://url-to-pdf-api.herokuapp.com/api/render?url=http://google.com">
   <button type="submit">Download!</button>
</form>

it seems you're not using same origin and that might have cause the problem.

Please use relative url rather than absolute url.

eg: download

This attribute only works for same-origin URLs.

your html

<a href="downloader.php">Download pdf</a>

you can use with php save with downloader.php

 $url = 'https://url-to-pdf-api.herokuapp.com/api/render?url=http://google.com';

    set_time_limit(0);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $r = curl_exec($ch);
    curl_close($ch);
    header('Expires: 0'); // no cache
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
    header('Cache-Control: private', false);
    header('Content-Type: application/force-download');
    header('Content-Disposition: attachment; filename="' . 'x.pdf' . '"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . strlen($r)); // provide file size
    header('Connection: close');
    echo $r;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM