我使用以下API从URL生成pdf:
https://url-to-pdf-api.herokuapp.com/api/render?url=http://google.com
我想要一个直接下载生成的pdf的PHP代码。以下是我的尝试:
a href =“https://url-to-pdf-api.herokuapp.com/api/render?url=http://google.com”download =“download”
但是,该文件不下载!
预期的输出应该是下载的pdf文件。
你的HTML
<a href="downloader.php">Download pdf</a>
你可以使用php保存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;
您可以使用
<form method="get" action="https://url-to-pdf-api.herokuapp.com/api/render?url=http://google.com">
<button type="submit">Download!</button>
</form>
看来你没有使用相同的来源,这可能会导致问题。
请使用相对网址而不是绝对网址。
例如:下载
此属性仅适用于同源URL。