codeigniter - 单击codeigniter中的下载按钮后将html转换为pdf

标签 codeigniter pdf

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

是否可以在codeigniter中实现?

我用的是 Tcpdf

Controller 代码是

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

最佳答案

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

How to Use

创建 PDF 需要设置 4 个选项;文件夹、文件名、文件和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>);

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

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

高级用法

没有理由不能构建 View 并将其传递给 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;
}

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

关于codeigniter - 单击codeigniter中的下载按钮后将html转换为pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44745594/

相关文章:

php - Codeigniter 和 'twice' 分组依据

php - mdate() 函数 codeigniter - 错误

python - 实现类错误并返回 0 文本

CodeIgniter 2 和 Ion Auth - 编辑自己的用户帐户

javascript - 如何调用附加在div中的angularjs函数

java - 使用 Java 和 pdfBox 在 pdf 中搜索美元金额

JavaFX有没有办法包含PDF文件

php - TCPDF 问题 - 双重编码

javascript - jsPDF 输出空白文档

使用 Controller 时 Codeigniter 500 内部服务器错误