使用dompdf创建多个PDF文件 [英] Creating multiple PDF files with dompdf

查看:67
本文介绍了使用dompdf创建多个PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个检查数据库并返回客户的每一行的脚本,该客户的帐户开始日期在特定日期.如果客户是在指定的日期创建的,那么客户行将传递给我的函数,以便根据数据库中客户的信息创建PDF.

I am writing a script that checks a database and returns each row of a customer that account start date is on a certain date. If the customer was created on the specified date then the customer row is passed to my function to create a PDF out of the customer's information in the database.

问题

我想为每个客户创建一个PDF的唯一方法是遍历数据并创建PDF.这样做时我有一个问题.

The only way I have thought up to create one PDF for each customer is to loop through the data and create the PDF. When doing so I have one problem.

1..仅创建第一个用户的PDF.与第二种无关.

1. Only the first user's PDF is created. Nothing happens pertaining to the second.

无法流式传输PDF:标头已发送

Unable to stream PDF: headers already sent

显示在浏览器中,并且第一个PDF下载.但是出于测试目的,应该在数据库中创建两个客户.我将数据回显到浏览器,并且两个客户都出现了.因此,我知道数据存在并且在运行时存在.

shows in the browser and the first PDF downloads. But for testing purposes there are two customers in the database that should be created. I echo out the data to the browser and both customers do show up. So I know the data exist and is there at run time.

示例

  // create instance of customer's that need to be created
    $a = new SelectTempOne;
     // obtain response from customers in database that have met timing requirement
    $response = $a->selectUserForOne();
        // loop through each row in the results and handle then pass to the pdf creation string
    foreach($response as $i => $row) {
    // statically typing template string to render email address of user
    $temp1 = '
        <!DOCTYPE html>
    <html>

    <head>
        <title>Template 1</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" type="text/css" media="screen" href="pdf-templates/temp1/temp1.css" />
    </head>

    <body>
        <div id="pdf_header">
            <table>
                <tr>
                    <td>'.$response[$i]['email'].'</td>
                </tr>
            </table>
        </div>
    </body>

    </html>
    ';

     // pass html to dompdf object
    $dompdf->loadHtml($temp1);

    // set option for the paper size and orientation
    $dompdf->setPaper('A4', 'letter');

    // Render the HTML as PDF
    $dompdf->render();

    //save the file to the server
    $output = $dompdf->output();
    file_put_contents('pdf/'.$response[$i]['email'].'.pdf', $output);


    // Output the generated PDF to Browser
    $dompdf->stream();
    exit;
    } // end of the for loop
    ?>

这行是我添加客户电子邮件的地方,

This line is where I add the customer's email,

 <td>'.$response[$i]['email'].'</td>

问题

当我遍历数据库返回的行时,我需要做些什么才能创建动态数量的PDF?

What do I need to do to be able to create a dynamic amount of PDF as I loop through the rows returned from the database?

*更新*

致命错误:未捕获的Dompdf \ Exception:未找到块级父级.不好.在/Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/Positioner/Inline.php:45堆栈跟踪:#0/Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(872):Dompdf \ Positioner \ Inline-> position(Object(Dompdf \ FrameDecorator \ Inline))

Fatal error: Uncaught Dompdf\Exception: No block-level parent found. Not good. in /Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/Positioner/Inline.php:45 Stack trace: #0 /Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(872): Dompdf\Positioner\Inline->position(Object(Dompdf\FrameDecorator\Inline))

Dompdf \ FrameDecorator \ AbstractFrameDecorator-> position()#2/Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(893):Dompdf \ FrameReflower \ Inline-> reflow(NULL)#3/Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/FrameReflower/Page.php(141):Dompdf \ FrameDecorator \ AbstractFrameDecorator-> reflow()#4/Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(893):Dompdf \ FrameReflower \ Page-> reflow(NULL)#5/Users/wuno/Dropbox/google/devop中/Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/Positioner/Inline.php在第45行

Dompdf\FrameDecorator\AbstractFrameDecorator->position() #2 /Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(893): Dompdf\FrameReflower\Inline->reflow(NULL) #3 /Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/FrameReflower/Page.php(141): Dompdf\FrameDecorator\AbstractFrameDecorator->reflow() #4 /Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(893): Dompdf\FrameReflower\Page->reflow(NULL) #5 /Users/wuno/Dropbox/google/devop in /Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/Positioner/Inline.php on line 45

推荐答案

Web浏览器通过请求文档来工作.您不能期望能够为用户提供多个文档.您可以一次调用 $ dompdf-> stream(); .您的选择:动态创建带有单个链接的列表页面,创建多页PDF或将PDF压缩在一起.

A web browser works by requesting a document. You can't expect to be able to serve multiple documents to a user. You get to call $dompdf->stream(); once and that's it. Your options: dynamically create a listing page with individual links, create a multi-page PDF, or zip the PDFs together.

好的,根据您的评论,您实际上并不想显示PDF.另外,我再看一遍,您的循环中有一个exit语句.这不会运行多次.另外,最好使用当您的代码中包含大量文本时,heredoc字符串,所以我就这样做了.

ok, based on your comment, you don't actually want to display the PDF. In addition, I took another look and you've got an exit statement in your loop. That's not going to run more than once. Also, it's a good idea to use heredoc strings when you have large blocks of text in your code, so I've done so.

<?php
// create instance of customer's that need to be created
$a = new SelectTempOne;
// obtain response from customers in database that have met timing requirement
$response = $a->selectUserForOne();
// loop through each row in the results and handle then pass to the pdf creation string
foreach($response as $i => $row) {

    // statically typing template string to render email address of user
    $temp1 = <<< HTML
<!DOCTYPE html>
<html>

<head>
    <title>Template 1</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" media="screen" href="pdf-templates/temp1/temp1.css" />
</head>

<body>
    <div id="pdf_header">
        <table>
            <tr>
                <td>$row[email]</td>
            </tr>
        </table>
    </div>
</body>

</html>
HTML;

    // pass html to dompdf object
    $dompdf->loadHtml($temp1);

    // set option for the paper size and orientation
    $dompdf->setPaper('A4', 'letter');

    // Render the HTML as PDF
    $dompdf->render();

    //save the file to the server
    $output = $dompdf->output();
    file_put_contents("pdf/$row[email].pdf", $output);

} // end of the for loop
?>

这篇关于使用dompdf创建多个PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆