簡體   English   中英

如何將渲染的 pdf(從 html 生成)保存到我的計算機?

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

我正在使用以下 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 文件。

您可以使用

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

看來您沒有使用相同的來源,這可能會導致問題。

請使用相對網址而不是絕對網址。

例如:下載

此屬性僅適用於同源 URL。

你的html

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

您可以使用 php 保存與 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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM