laravel 如何为laravel dompdf中的每一页添加页码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41272819/
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
How to add page number for every page in laravel dompdf?
提问by samuel toh
I get from here : https://github.com/barryvdh/laravel-dompdf
我从这里得到:https: //github.com/barryvdh/laravel-dompdf
My controller is like this :
我的控制器是这样的:
public function listdata()
{
$pdf=PDF::loadView('print_tests.test_pdf');
$pdf->setPaper('L', 'landscape');
return $pdf->stream('test_pdf.pdf');
}
My view is like this :
我的观点是这样的:
<table>
<tr>
<th>header1</th>
<th>header2</th>
<th>header3</th>
</tr>
<tr>
<td>content-row-1</td>
<td>content-row-1</td>
<td>content-row-1</td>
</tr>
<tr>
<td>content-row-2</td>
<td>content-row-2</td>
<td>content-row-2</td>
</tr>
</table>
I want every page there is a page number
我希望每一页都有一个页码
Is there any people who can help me?
有没有人可以帮助我?
采纳答案by AddWeb Solution Pvt Ltd
Follow below steps to achieve it:
- Enable DOMPDF_ENABLE_PHP
from /config/dompdf.php
- Publish vendor file via php artisan vendor:publish command
- Pass $pdf
object from controller:
- Add below code inside the view file:
请按照以下步骤来实现它:
-启用DOMPDF_ENABLE_PHP
从/config/dompdf.php
-通过发布供应商档案php artisan vendor:publish command
-山口$pdf
对象从控制器:
-添加下面的视图文件中的代码:
<script type="text/php">
if ( isset($pdf) ) {
$font = Font_Metrics::get_font("helvetica", "bold");
$pdf->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0,0,0));
}
</script>
You can get more idea from Page count and page number
您可以从页数和页码中获得更多信息
回答by larp
Try this one.
试试这个。
<style>
.pagenum:before {
content: counter(page);
}
</style>
<body>
<span class="pagenum"></span>
<table>
<tr>
<th>header1</th>
<th>header2</th>
<th>header3</th>
</tr>
<tr>
<td>content-row-1</td>
<td>content-row-1</td>
<td>content-row-1</td>
</tr>
<tr>
<td>content-row-2</td>
<td>content-row-2</td>
<td>content-row-2</td>
</tr>
</table>
</body>
回答by Jignesh Joisar
you can do like that for example..
例如,你可以这样做..
in your controller :
在您的控制器中:
$pdf = app('dompdf.wrapper');
$pdf->getDomPDF()->set_option("enable_php", true);
$data = ['title' => 'Testing Page Number In Body'];
$pdf->loadView('welcomeView', $data);
in your welcomeView.blade.php
在你的 welcomeView.blade.php
<html>
<body>
<h1> {{ $title }} </h1>
<script type="text/php">
if (isset($pdf)) {
$x = 250;
$y = 10;
$text = "Page {PAGE_NUM} of {PAGE_COUNT}";
$font = null;
$size = 14;
$color = array(255,0,0);
$word_space = 0.0; // default
$char_space = 0.0; // default
$angle = 0.0; // default
$pdf->page_text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle);
}
</script>
</body>
</html>