本文介绍了mPDF中动态生成的页面的页码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用mPDF与Codeigniter来创建PDF文件。我在添加页码时遇到问题。
我有一些通过数据库查询生成的结果集,我将它们传递到我的html文件以显示该数据。根据结果​​的数量创建并转换为pdf格式与页数。问题是,我不能添加页码,当它生成多个页面。
我使用这个操作的基本示例。
请帮助我!



我的控制器代码

  $ result_1 = $ this-> Vessel_model-> getManifestVesselDetails($ vesselId,$ containerId); 
$ result_2 = $ this-> Vessel_model-> getVesselDetailsByContainer($ vesselId,$ containerId);
$ result_3 = $ this-> Vessel_model-> getDDByVesselContainer($ vesselId,$ containerId);

$ data ['agentDetails'] = $ this-> Vessel_model-> getShippingAgents();
$ data ['DD_SDNumber'] = $ this-> DD_SDNumber;
$ data ['DDdetails'] = $ result_3;
$ data ['containerDetails'] = $ result_2;
$ data ['vesselcontainerDetails'] = $ result_1;

$ html = $ this-> load-> view('reports / tri_shipping_manifest_PDF',$ data,true);
//这是用户将要下载的PDF文件名
$ pdfFilePath = APPPATH。'/'。date('Ym-d')。$ result_1-> vessel .'-'。 $ result_1-> serialno。。pdf;
$ pdfFilePath3 = date('Y-m-d')。$ result_1-> vessel .'-'。$ result_1-> serialno。。pdf

$ this-> load-> library('pdfreports');
$ this-> pdf = $ this-> pdfreports-> load();
// $ this-> pdf-> setFooter('{PAGENO}');
$ this-> pdf-> AddPage('L','',1,'i','on');
$ this-> pdf-> WriteHTML($ html,0);
$ this-> pdf->输出($ pdfFilePath3,D);
$ this-> pdf->输出($ pdfFilePath,F);


解决方案

我不知道当前代码应该是什么样子,因为我不知道mPDF,但我建议你使用domPdf与Codeigniter,因为比这个库要简单得多。
您可以在这里找到

因为我们不知道这些变量的输出是什么,什么应该打印在pdf,我只能建议一些一般的代码创建domPdf



create helper:

 <?php if(!defined('BASEPATH')) exit('No direct script access allowed'); 
function pdf_create($ html,$ filename ='',$ stream = TRUE){
require_once(dompdf / dompdf_config.inc.php);
$ dompdf = new DOMPDF();
$ dompdf-> load_html($ html);
$ dompdf-> render();
$ dompdf-> set_paper(A4);
if($ stream){
$ dompdf-> stream($ filename。。pdf,1);
} else {
return $ dompdf-> output();
}
}
?>

create controller for pdf

 <?php 
$ data ['store'] = $ res;
$ this-> load-> helper(array('dompdf','file'));
$ html = $ this-> load-> view('store / sales_pdf',$ data,true);
$ html。= $ this-> load-> view('footer');
$ filename =salesbill。$ id;
pdf_create($ html,$ filename);
$ data = pdf_create($ html,'',false);
Write_file('name',$ data);
?>

您的PDF HTML看起来像这样

 < html> 
< head>
< style>
@page {margin:180px 50px; }
#header {position:fixed; left:0px; top:-180px; right:0px; height:150px;背景颜色:橙色; text-align:center; }
#footer {position:fixed; left:0px; bottom:-180px; right:0px; height:150px;背景颜色:lightblue; }
#footer .page:after {content:counter(page,upper-roman); }
< / style>
< body>
< div id =header>
< h1> Widgets Express< / h1>
< / div>
< div id =footer>
< p class =page>页<?php $ PAGE_NUM?>< / p>
< / div>
< div id =content>
< p>第一页< / p>
< p style =page-break-before:always;>第二页< / p>
< / div>
< / body> ;,
< / html>

这个答案是基于
这里也很漂亮domPdf
我希望这个答案可以帮助你


I am using mPDF with Codeigniter for creating PDF files. I have a problem when adding page number.I have some result sets that are generated via database queries and I pass them to my html file to display that data. According to the number of results it creates and converts to pdf format with number of pages. Problem is I cannot add page numbers when it generates more than one page.I am using the basic examples for this operation.Please help me on this!

My controller code

$result_1 = $this->Vessel_model->getManifestVesselDetails($vesselId,$containerId);
$result_2 = $this->Vessel_model->getVesselDetailsByContainer($vesselId,$containerId);
$result_3 = $this->Vessel_model->getDDByVesselContainer($vesselId,$containerId);

$data['agentDetails'] = $this->Vessel_model->getShippingAgents();        
$data['DD_SDNumber'] = $this->DD_SDNumber;
$data['DDdetails'] = $result_3;
$data['containerDetails'] = $result_2;
$data['vesselcontainerDetails'] = $result_1;

$html = $this->load->view('reports/tri_shipping_manifest_PDF', $data, true);
//this the the PDF filename that user will get to download
$pdfFilePath = APPPATH.'/'.date('Y-m-d').$result_1->vessel.'-'.$result_1->serialno.".pdf";
$pdfFilePath3 = date('Y-m-d').$result_1->vessel.'-'.$result_1->serialno.".pdf";

$this->load->library('pdfreports');
$this->pdf = $this->pdfreports->load();
//$this->pdf->setFooter('{PAGENO}');
$this->pdf->AddPage('L','',1,'i','on'); 
$this->pdf->WriteHTML($html,0);
$this->pdf->Output($pdfFilePath3, "D");
$this->pdf->Output($pdfFilePath, "F");
解决方案

I don't know how current code should look like, because i don't know much about mPDF, but i would suggest you to use domPdf with Codeigniter, because is much simpler then this library.You can find domPdf here

Since we don't know what is the output for those variables and what should be printed in pdf, i can only suggest some generally code created with domPdf

create helper:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    function pdf_create($html, $filename='', $stream=TRUE){
      require_once("dompdf/dompdf_config.inc.php");         
      $dompdf = new DOMPDF();
      $dompdf->load_html($html);
      $dompdf->render();
      $dompdf->set_paper("A4");
      if ($stream) {
        $dompdf->stream($filename.".pdf",1);
      } else {
        return $dompdf->output();
      }
    }
?>

create controller for pdf

<?php
  $data['store']=$res;  
  $this->load->helper(array('dompdf', 'file'));
  $html = $this->load->view('store/sales_pdf', $data, true);
  $html.= $this->load->view('footer');
  $filename="salesbill".$id;
  pdf_create($html, $filename);
  $data = pdf_create($html, '', false);
  write_file('name', $data); 
?>

And your pdf HTML would look something like this

<html>
  <head>
   <style>
     @page { margin: 180px 50px; }
     #header { position: fixed; left: 0px; top: -180px; right: 0px; height: 150px; background-color: orange; text-align: center; }
     #footer { position: fixed; left: 0px; bottom: -180px; right: 0px; height: 150px; background-color: lightblue; }
     #footer .page:after { content: counter(page, upper-roman); }
   </style>
  <body>
   <div id="header">
     <h1>Widgets Express</h1>
   </div>
   <div id="footer">
     <p class="page">Page <?php $PAGE_NUM ?></p>
   </div>
   <div id="content">
     <p>the first page</p>
     <p style="page-break-before: always;">the second page</p>
   </div>
 </body>
 </html>

This answer is based on this stackoverflow questionAlso here is nice domPdf debug helper pageI hope this answer helps you out

这篇关于mPDF中动态生成的页面的页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 06:14