PDFcrowd PHP PDF Generator API创建href触发器

时间:2015-01-13 20:54:03

标签: php

我使用以下示例代码从URL生成PDF,您会注意到PDF是自动生成的。我需要能够更改此功能以使用按钮或href来触发$ pdf变量。您会注意到代码的两个版本,一个是我的href尝试,但似乎不起作用但显示白屏。

工作正常,但会自动运行:

<?php
require 'pdfcrowd.php';

try
{   
    // create an API client instance
    $client = new Pdfcrowd("user", "apikey");

    // convert a web page and store the generated PDF into a $pdf variable
    $pdf = $client->convertURI('http://www.home-tech.com/'.$_SERVER['PHP_SELF']);

    // set HTTP response headers
    header("Content-Type: application/pdf");
    header("Cache-Control: max-age=0");
    header("Accept-Ranges: none");
    header("Content-Disposition: attachment; filename=\"google_com.pdf\"");

    // send the generated PDF 
    echo $pdf;
}
?>
<html>
<body>
hello world
<input name="" type="text" value="8989">
</body>
</html>

我尝试使用href:

启动PDF生成
<?php
require 'pdfcrowd.php';

//try
//{   
    // create an API client instance
    $client = new Pdfcrowd("user", "apikey");

    // convert a web page and store the generated PDF into a $pdf variable
    $pdf = $client->convertURI('http://www.home-tech.com/'.$_SERVER['PHP_SELF']);

    // set HTTP response headers
    header("Content-Type: application/pdf");
    header("Cache-Control: max-age=0");
    header("Accept-Ranges: none");
    header("Content-Disposition: attachment; filename=\"google_com.pdf\"");

    // send the generated PDF 
    // echo $pdf;
//}
?>
<html>
<body>
hello world
<input name="" type="text" value="8989">
<a href="<?php echo $pdf; ?>">PDF Create</a>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

假设这被命名为file.pdf

<?php

if($_GET['pdf']=='YES'){

require 'pdfcrowd.php';

    // create an API client instance
    $client = new Pdfcrowd("user", "apikey");

    // convert a web page and store the generated PDF into a $pdf variable
    $pdf = $client->convertURI('http://www.home-tech.com/'.$_SERVER['PHP_SELF']);

    // set HTTP response headers
    header("Content-Type: application/pdf");
    header("Cache-Control: max-age=0");
    header("Accept-Ranges: none");
    header("Content-Disposition: attachment; filename=\"google_com.pdf\"");

    // send the generated PDF 
     echo $pdf;
exit();
}
?>
<html>
<body>
hello world

<a href="file.pdf?pdf=YES">PDF Create</a>
</body>
</html>