更新时间:2023-02-12 21:58:50
您的循环工作正常.您向 PDF 添加页面的方式可能是错误的.显然,您一次又一次地覆盖了一页,而不是附加新的一页.
编辑
我从未使用过 dompdf.快速浏览文档让我觉得您创建了一个类似于 HTML 标记的内容,然后将其转换为 PDF,我做对了吗?
示例代码
$html = <<<HTML<头><style type="text/css">/* 你的文档样式放在这里 */</风格>头部><身体>HTML;while ( $row = $dbResult->fetch_assoc() ) {$html .= '<div class="teacherPage">'.$row['name']//你的教师文档放在这里'</div>';}$html .= '</body></html>';$dompdf = 新的 DOMPDF();$dompdf->load_html($html);$dompdf->render();$dompdf->stream("sample.pdf");
如果您想知道不寻常的语法 $var = <<<HTML \r\nHTML
,那就是 heredoc.当您有很多外来内联代码时,使用heredoc 会更舒服,这可以有变量 {$varname}
并且您无需担心引号.您需要确保的是,heredoc close HTML
是在一个新行中,而不是缩进.
EDIT2
仍然不太确定,您使用的是哪个库.我发现这个扩展看起来很不错,它被称为 dompdf,就像你在问题中所说的那样.>
你的最新评论表明你到目前为止还没有解决你的问题,所以我决定添加一些更多的信息来让你达到目标.
免责声明:我不会编写功能性代码,也不会对此进行测试,但以下提示将推动您朝着正确的方向完成工作.
dompdf 能够读取输入文档的 CSS2 和 CSS3 属性.
上述 while
循环中的每个循环代表一位老师,他们每个人在输出文档中都有自己的页面.
我将页面放入具有 teacherPage
类的 div 容器中.您可以在此容器中填充您希望向教师显示的所有信息.
现在我们需要做的就是告诉dompdf每个teacherPage
都是一个新页面.这可以使用 @page
标记随 CSS3 一起提供
我在上面的示例文档中添加了一个空的 css 容器 <style type="text/css"></style>
,这就是页面样式应该去的地方.>
示例 CSS
@page老师{尺寸:A4纵向;边距:2cm;}.teacherPage {页:老师;分页后:总是;}
使用@page
,您可以定义一个命名页面teacher
,它可以具有对整个页面容器有效的属性.
page-break-after: always
将在每个容器之后开始一个新页面
希望这会有所帮助,祝您尝试愉快:)
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
$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.
Please help. It is kinda urgent.
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.
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?
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 HTML
is in a new line and not indented.
EDIT2
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.
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.
Each cycle in the while
loop above represents one teacher whith each of them getting a own page in the output document.
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.
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
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.
The example 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 :)