php 如何使用dompdf创建多个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14089883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:36:31  来源:igfitidea点击:

How to create several pages with dompdf

phpmysqlarraysdompdf

提问by I Perfect

I am having some trouble with multidimensional array and its value.

我在使用多维数组及其值时遇到了一些麻烦。

What i am looking for is , from my query I am searching teachers name in the array. And after that i want to create a pdf using dompdf. The problem is with looping. I am not able to create a proper loop which will work the way I want it to work. My sample query is

我正在寻找的是,从我的查询中,我正在数组中搜索教师姓名。之后我想使用dompdf创建一个pdf。问题在于循环。我无法创建一个正确的循环,它将按照我希望的方式工作。我的示例查询是

    $q11 = "select id from teachers order by teacher ";
    $r11 = mysql_query($q11) or die(mysql_error());
    while($rows11 = mysql_fetch_array($r11)){
        $teacher = $rows11['id'];
        $dompdf->"It will start working";
    }

Now i know , this code is confusing, but what i want is, it should create dompdf for every teacher in one single pdf file. Like from the query it should fetch teachers, and for each teacher it should create a dompdf page. Currently it is making just one page according to the last value that my query has search.

现在我知道,这段代码令人困惑,但我想要的是,它应该在一个 pdf 文件中为每个老师创建 dompdf。就像从查询中获取教师一样,它应该为每个教师创建一个 dompdf 页面。目前,它仅根据我的查询搜索的最后一个值制作一页。

Please help. It is kinda urgent.

请帮忙。有点急啊

回答by Michel Feldheim

Your loop is working fine. The way you add pages to your PDF is probably wrong. Apparently you are overwriting one page again and again instead of attaching a new one.

你的循环工作正常。您向 PDF 添加页面的方式可能是错误的。显然,您一次又一次地覆盖一页,而不是附加新的一页。

EDIT

编辑

I've never used dompdf. A quick look into the docs let me think you create something like a HTML markup which then is converted into PDF, did I get this right?

我从来没有用过 dompdf。快速浏览文档让我觉得你创建了一个类似于 HTML 标记的东西,然后将其转换为 PDF,我做对了吗?

Example code

示例代码

$html = <<<HTML
  <html>
      <head>
            <style type="text/css">
                /* Your document styling goes here */
            </style>
      </head>
      <body>
HTML;

while ( $row = $dbResult->fetch_assoc() ) {
    $html .= '<div class="teacherPage">'
                 . $row['name'] // your teacher document goes here
             '</div>';
}

$html .= '</body></html>';

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");

If you wonder about the unusual syntax $var = <<<HTML \r\nHTML, that's a heredoc. It's just more comfortable to use heredocs when you have a lot of alien inline code, this can have variables {$varname}and you don't need to worry about quotes. All you need to make sure, is that heredoc closer HTMLis in a new line and not indented.

如果您想知道不寻常的语法$var = <<<HTML \r\nHTML,那就是heredoc。当你有很多外来内联代码时,使用heredoc 会更舒服,这可以有变量{$varname},你不需要担心引号。您需要确保的是,heredoc closeHTML是在一个新行中,而不是缩进。

EDIT2

编辑2

Still not too sure, which library you are using. I find this extensionlooking pretty good and it's called dompdf, just like you said in your question.

仍然不太确定,您正在使用哪个库。我发现这个扩展看起来很不错,它被称为 dompdf,就像你在你的问题中所说的那样。

Your latest comment indicates you did not solve your problem so far, so I decided to add some more information to get you to the target.

您的最新评论表明到目前为止您还没有解决您的问题,因此我决定添加更多信息以使您达到目标。

Disclaimer: I am not going to write functional code and I will not test this, but the following hints will push you into the right direction to get your stuff done.

免责声明:我不会编写功能性代码,也不会对此进行测试,但以下提示将推动您朝着正确的方向完成工作。

dompdf is able to read CSS2 and CSS3 properties of your input document.

dompdf 能够读取输入文档的 CSS2 和 CSS3 属性。

Each cycle in the whileloop above represents one teacher whith each of them getting a own page in the output document.

while上面循环中的每个循环代表一位老师,他们每个人在输出文档中都有自己的页面。

I put the page into a div container with the class teacherPage. You can fill this container with all the information you want to have displayed for a teacher.

我将页面放入带有 class 的 div 容器中teacherPage。您可以使用要向教师显示的所有信息填充此容器。

Now all we need to do, is to tell dompdf each teacherPageis a new page. This can be done using @pagemarkup shipped with CSS3

现在我们需要做的就是告诉 dompdf 每个teacherPage都是一个新页面。这可以使用CSS3 附带的@page标记来完成

I added an empty css container <style type="text/css"></style>to the example document above, that's where the page styling should go to.

<style type="text/css"></style>在上面的示例文档中添加了一个空的 css 容器,这就是页面样式应该去的地方。

The example CSS

示例 CSS

@page teacher {
  size: A4 portrait;
  margin: 2cm;
}

.teacherPage {
   page: teacher;
   page-break-after: always;
}

With @pageyou can define a named page teacher, which can have properties valid for the whole page container.

随着@page您可以定义一个名为页面teacher,它可以适用于整个页面容器的属性。

page-break-after: alwayswill begin a new page after each container

page-break-after: always将在每个容器之后开始一个新页面

Hope this helps, have fun trying :)

希望这会有所帮助,祝您尝试愉快:)

回答by Walter Caraza

Apparently, no overflow problems and the results are taking more than the permitted margin of the sheet. What you can do is to separate data from the parent in another table and put between them:

显然,没有溢出问题,结果超出了纸张的允许边距。您可以做的是将数据与另一个表中的父数据分开并放在它们之间:

<div style="page-break-before: always;"></div>