如何为pdf配置phpexcel [英] How to configure phpexcel for pdf
问题描述
我一直在搜索官方文档,但我似乎无法将所有内容都放在脑海中(我是编程新手).我设法使用了excel库,并且该库可以超级正常地工作,但现在我想为用户提供选择在.xls或.pdf文件中进行下载的机会.
i've been searching in the official docs but i can't seem to put all the things together in my mind (i'm new to programming). I managed to use the excel library and it's working super, but now i want to offer the user the chance to choose between downloading the file in .xls or in .pdf.
我正在使用Codeigniter和WAMP.
I'm using Codeigniter and WAMP.
我下载了PHPExcel,它在我的C:目录中.
I downloaded PHPExcel, and it's on my C: directory.
后来,我将类:PHPExcel(文件夹)和PHPExcel.php复制到了我的codeigniter的第三方文件夹中.
Later i copied Classes: PHPExcel (folder) and PHPExcel.php to my codeigniter's third party folder.
然后,在Codeigniter的应用程序库中,我创建了一个名为pdf.php的文件,并在其中复制了原始PHPExcel文档(21pdf.php)的代码
Then, inside Codeigniter's Application, Libraries, i created a file named pdf.php and copied in there the code from the original PHPExcel docs (21pdf.php)
pdf.php
/** PHPExcel_IOFactory */
require_once dirname(__FILE__) . '/../Classes/PHPExcel/IOFactory.php';
$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
$rendererLibrary = 'tcPDF5.9';
$rendererLibraryPath = '/php/libraries/PDF/' . $rendererLibrary;
if (!PHPExcel_Settings::setPdfRenderer(
$rendererName,
$rendererLibraryPath
)) {
die(
'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
EOL .
'at the top of this script as appropriate for your directory structure'
);
}
我真的不知道如何配置它,我的大脑现在已经烧毁了.任何帮助将不胜感激!
I don't really know how to configure it, my brain is burned right now. Any help would be very appreciated!
推荐答案
好的,现在我可以使用此工具了,花了我几天时间,但终于实现了.
ok so now i have this thing working, took me several days but finally achieved it.
为了在Codeigniter中配置phpexcel以生成pdf报告,我必须:
In order to configure phpexcel in Codeigniter for generating pdf reports i had to:
1).下载PHPExcel,然后将内容从类"文件夹复制到我的框架Codeigniter中的另一个文件夹(application/third_party).在类"文件夹中,您将找到另一个名为"PHPExcel"的文件夹和一个具有相同名称的.php文件.
1). Download PHPExcel, and copy the content from the folder "Classes" to another folder in my framework Codeigniter (application/third_party). Inside of Classes folder, you will find another folder named "PHPExcel" and a .php file with the same name.
2).然后,在应用程序/库中,我使用类构造函数创建了一个文件excel.php:
2). Then, inside application/libraries i created a file excel.php with the class constructor:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* =======================================
* Author : Muhammad Surya Ikhsanudin
* License : Protected
* Email : mutofiyah@gmail.com
*
* Dilarang merubah, mengganti dan mendistribusikan
* ulang tanpa sepengetahuan Author
* =======================================
*/
require_once APPPATH."/third_party/PHPExcel.php";
class Excel extends PHPExcel {
public function __construct() {
parent::__construct();
}
}
3). nex继续使用pdf.首先,您应该选择要使用的库.我选择使用dompdf https://github.com/dompdf/dompdf 并将其安装在application/libraries中
3). nex to move on to pdf. First you should choose which library you want to use. I chose to use dompdf https://github.com/dompdf/dompdf and install this in application/libraries
4).创建了一个文件名pdf.php(将其放置在所需的位置),并在其中放入了它(基于官方phpexcel文档中提供的示例:21pdf.php和01simple-download-pdf.php,您可以在测试中找到该文件) PHPExcel中的文件夹):
4). created a file name pdf.php (place it where you want) and in it i put this (based on the examples given in the official phpexcel docs: 21pdf.php and 01simple-download-pdf.php that you can find in the test folder in PHPExcel):
$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
$rendererLibrary = 'dompdf_0-6-0_beta3';
$rendererLibraryPath = dirname(__FILE__).'/'. $rendererLibrary;
'dompdf_0-6-0_beta3'是包含用于渲染为pdf的库的文件夹的名称
'dompdf_0-6-0_beta3' is the name of the folder that contains the library for rendering into pdf
dirname( FILE ).'/'. $ rendererLibrary是查找此"dompdf_0-6-0_beta3"文件夹的路径
dirname(FILE).'/'. $rendererLibrary is the path for finding this 'dompdf_0-6-0_beta3' folder
5).在我的admin.php中构建方法以调用此函数.首先,我要做的是建立excel表并放入一些数据.之后,我使用布尔值来查看用户是否需要.xls或.pdf.如果他要使用.pdf,那么用于生成具有该扩展名的报告的代码就是:
5). build the method in my admin.php to call to this function. What i did in the first place was to build the excel tables and to put some data. After that, i use a boolean to see if user wants in .xls or .pdf. If he wants in .pdf then the code for generating the report with that extension then the code is this:
require_once (realpath(dirname(dirname(dirname(__FILE__))))."/libraries/pdf.php");
$filename=$rDate.'_Reporte_Usuarios_front'.'.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
// $objWriter = new PHPExcel_Writer_PDF($objPHPExcel);
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'PDF');
$objWriter->setSheetIndex(0);
$objWriter->save('php://output');
当我的错误是找不到渲染库时,我发布了此问题.这就是为什么我试图使用phpexcel包装而不是propper库的原因.因此,在那之后,必须在路径上稍作努力,然后才能做到.
I posted this question when my error was that render library couldn't be found. That's why i was trying to use the phpexcel wrapper and not the propper library. So after that had to fight a little bit with the path but then made it.
此问题修复后,出现另一个错误:DomPDF中的无法加载PDF渲染库"(来自PHPExcel).所以我发现这一行:
After that fixed, another error came up: 'Unable to load PDF Rendering library' in DomPDF (from PHPExcel). So i found out that this line:
$pdfRendererClassFile = PHPExcel_Settings::getPdfRendererPath() . '/dompdf/dompdf_config.inc.php';
我缺少包含dompdf_config.inc.php的文件夹,因为完整路径为:
i was missing the folder containing dompdf_config.inc.php, because complete path is:
应用程序/库/dompdf_0-6-0_beta3/dompdf/材料
application/libraries/dompdf_0-6-0_beta3/dompdf/stuff
所以只需添加'dompdf'并完成:)
so just added 'dompdf' and done :)
这篇关于如何为pdf配置phpexcel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!