-2

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.

Vikrant
  • 4,920
  • 17
  • 48
  • 72
Amit
  • 77
  • 1
  • 11

3 Answers3

0

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>
Şafak Çıplak
  • 889
  • 6
  • 12
0

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.

Edison Augusthy
  • 1,535
  • 17
  • 32
0

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;
Şafak Çıplak
  • 889
  • 6
  • 12