【问题标题】:Converting html to pdf after clicking on download button in codeigniter单击codeigniter中的下载按钮后将html转换为pdf
【发布时间】:2017-06-25 10:51:25
【问题描述】:

我创建了一个简历生成器应用程序。用户输入所有必需的详细信息,我将所有详细信息存储在数据库中。然后我获取这些记录并将其显示在不同的模板中。我创建了 3 个模板。现在我想以 pdf 格式下载和以 docx 格式下载两个下载按钮。一旦用户单击任何按钮文件,将以他们选择的格式下载。 pdf 或 docx。

是否可以在codeigniter中实现?

我用过 Tcpdf

控制器代码是

the code written in controller download $this - > load - > library("Pdf");
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set auto page breaks
$pdf - > SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf - > setImageScale(PDF_IMAGE_SCALE_RATIO);
// Add a page
// This method has several options, check the source code documentation for more information.
$pdf - > AddPage();
// Set some content to print 
$html = $this - > load - > view('resume/res_template1_view', array('left_sidebar' => 'sidebar/left_sidebar', '             right_sidebar' => 'sidebar/right_sidebar'), $data, TRUE);
// Print text using writeHTMLCell()
$pdf - > writeHTML($html, 0, 0, '', '', 0, 1, 0, true, '', true);
//$pdf->writeHTML($html, true, false, true, false, ''); 

// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf - > Output('resume.pdf', 'D');

【问题讨论】:

  • 谷歌搜索类似“php html2pdf”的内容。关于.docx 文件PHPOffice/PHPWord 可能是您的选择。
  • 你也可以使用parall.ax/products/jspdf这个。非常有据可查。
  • 您可以使用Mpdf https://davidsimpson.me/2013/05/19/using-mpdf-with-codeigniter/http://www.w3school.info/2016/02/08/convert-html-to-pdf-in-codeigniter-using-mpdf/

标签: codeigniter pdf


【解决方案1】:

您可能需要在 CI 中使用一些库来实现这一点。请检查以下。 您可以将 Html2Pdf 用于 CI3 库。 https://github.com/shyamshingadiya/HTML2PDF-CI3

如何使用

您需要设置 4 个选项才能创建 PDF;文件夹、文件名、纸张和 HTML

//Load the library
$this->load->library('html2pdf');

//Set folder to save PDF to
$this->html2pdf->folder('./assets/pdfs/');

//Set the filename to save/download as
$this->html2pdf->filename('test.pdf');

//Set the paper defaults
$this->html2pdf->paper('a4', 'portrait');

//Load html view
$this->html2pdf->html(<h1>Some Title</h1><p>Some content in here</p>);

create 函数有两个选项'save''download'。保存只会将 PDF 写入您在设置中选择的文件夹。下载将强制 PDF 在客户端浏览器中下载。

//Create the PDF
$this->html2pdf->create('save');

高级用法

没有理由不能构建视图并将其传递给html() 函数。另请注意,如果您选择 'save' 选项,create() 函数将返回保存的路径。

$data = array(
    'title' => 'PDF Created',
    'message' => 'Hello World!'
);

//Load html view
$this->html2pdf->html($this->load->view('pdf', $data, true));

if($path = $this->html2pdf->create('save')) {
    //PDF was successfully saved or downloaded
    echo 'PDF saved to: ' . $path;
}

请检查上述解决方案。如果它不起作用,请告诉我。

【讨论】:

  • 我使用了 Tcpdf。下载工作正常。但是如果我将 vlaue 作为 $this->load->view 传递给 $html 变量,下载的 pdf 文件是空的。
  • 如果我直接在 $html 中写一些 html 下载的 pdf 包含那些 html css 值
  • 好的,现在您可以打印您的$html 并检查您是否获得了价值。
  • 视图没有加载到 $html 中。不能直接将视图加载到变量中吗?
  • $variable = $this-&gt;load-&gt;view('view_name',$data, true);你可以用这个。
猜你喜欢
  • 2018-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 2018-02-11
  • 2013-02-16
  • 1970-01-01
相关资源
最近更新 更多