如何创建多个页面与DOMPDF [英] How to create several pages with dompdf

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

问题描述

我有一些麻烦多维数组和它的价值。

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

我所寻找的是,从我的查询我在数组中寻找老师的名字。之后,我想创建一个使用DOMPDF PDF文件。问题是与循环。我不能够创造一个合适的循环,将工作,我希望它的工作方式。我的样本查询

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

    $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";
    }

现在我知道,这code是混乱的,但我要的是,它应该在一个单一的PDF文件每一位教师创造DOMPDF。从查询像它应该取的教师,并为每个教师应该创建一个页面DOMPDF。目前它是按照我的查询具有搜索的最后一个值使得只有一页。

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的方式也许是错误的。显然,你一次又一次地改写一个页面,而不是安装一个新的。

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.

修改

我从来没有使用DOMPDF。快速查看的文档,让我觉得你创造的东西就像一个HTML标记,然后转换成PDF,为什么我得到这个权利?

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?

示例code

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");

如果你想知道的不寻常的语法是$ var =&LT;&LT;&LT; HTML \\ r \\ nHTML ,这是一个<一个href=\"http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc\">heredoc.这只是更舒服,当你有很多外国人直列code,这可以有变量 {$ VARNAME} 使用here文档,你不必担心引号。所有你需要确保,是定界符接近 HTML 是一个新行,而不是缩进。

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 HTML is in a new line and not indented.

EDIT2

还是不太清楚,你所使用的库。我觉得这个扩展寻找pretty好,这就是所谓DOMPDF,就像你说的在你的问题。

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

您最新的评论表明您没有解决您的问题,到目前为止,所以我决定添加一些更多的信息,让你的目标。

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.

免责声明:我不打算写功能code,我将对此进行测试,但下面的提示将你推到正确的方向,让您的东西做

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

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

,而以上循环再presents一位老师蒙山他们每个人的输出文档中得到一个自己的网页每个周期。

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

我把这个页面到一个div容器类 teacherPage 。您可以填写此容器所有你希望显示了老师的信息。

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.

现在我们需要做的,就是要告诉每DOMPDF teacherPage 是一个新的页面。这是可以做到用 @页标记随CSS3

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

我添加了一个空的CSS容器&LT;风格类型=文/ CSS&GT;&LT; /风格&GT; 上面的例子文件,这其中的页面样式应该去。

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.

的例子CSS

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

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

使用 @页您可以定义一个名为页教师,它可以适用于整个页面容器属性

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

页面突破后:总是将每个容器后,开始了新的一页。

page-break-after: always will begin a new page after each container

希望这有助于,有有趣的尝试:)

Hope this helps, have fun trying :)

这篇关于如何创建多个页面与DOMPDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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