/** * 导出Pdf * @param request 请求对象 * @param response 响应对象 * @param pager dtGrid对象 * @param exportDatas 导出的数据 * @param fileName 文件名 * @throws Exception */ static function exportPdf($pager, $exportDatas, $fileName) { // 定义PDF文件 $pdf = new PDF_Chinese("L"); $pdf->AddGBFont(); $pdf->AddPage(); $pdf->SetFont('GB', '', 9); $pdf->SetFillColor(47, 151, 210); $pdf->SetDrawColor(33, 33, 33); $pdf->SetTextColor(255, 255, 255); // 循环一次获取出宽度 $widths = array(); if ($pager["exportColumns"] != null && count($pager["exportColumns"]) > 0) { // 循环写入表头 foreach ($pager["exportColumns"] as $column) { array_push($widths, $pdf->GetStringWidth(iconv("UTF-8", "GBK", $column["title"])) + 6); } // 判断表中是否有数据 if ($exportDatas != null && count($exportDatas) > 0) { // 循环写入表中数据 foreach ($exportDatas as $record) { $i = 0; foreach ($pager["exportColumns"] as $column) { $content = $record[$column["id"]]; // 如果内容未被处理则进行格式化 if (!$pager["exportDataIsProcessed"]) { $content = DtGridUtils::formatContent($column, $content); } $width = $pdf->GetStringWidth(iconv("UTF-8", "GBK", $content)) + 6; if ($widths[$i] < $width) { $widths[$i] = $width; } $i++; } } } } // 判断一下表头数组是否有数据 if ($pager["exportColumns"] != null && count($pager["exportColumns"]) > 0) { // 循环写入表头 $i = 0; foreach ($pager["exportColumns"] as $column) { $pdf->Cell($widths[$i], 6, iconv("UTF-8", "GBK", $column["title"]), 1, 0, "C", 1); $i++; } $pdf->SetTextColor(44, 44, 44); $pdf->Ln(); // 判断表中是否有数据 if ($exportDatas != null && count($exportDatas) > 0) { // 循环写入表中数据 foreach ($exportDatas as $record) { $i = 0; foreach ($pager["exportColumns"] as $column) { $content = $record[$column["id"]]; // 如果内容未被处理则进行格式化 if (!$pager["exportDataIsProcessed"]) { $content = DtGridUtils::formatContent($column, $content); } $pdf->Cell($widths[$i], 6, iconv("UTF-8", "GBK", $content), 1, 0, "C", 0); $i++; } $pdf->Ln(); } } } $pdf->Output($fileName . ".pdf", "D"); }
function Cell($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = 0, $link = '') { if ($this->isUnicode) { $txt = mb_convert_encoding($txt, FPDF_UNICODE_ENCODING, $this->charset); $oEnc = mb_internal_encoding(); mb_internal_encoding(FPDF_UNICODE_ENCODING); $this->UniCell($w, $h, $txt, $border, $ln, $align, $fill, $link); mb_internal_encoding($oEnc); } else { parent::Cell($w, $h, $txt, $border, $ln, $align, $fill, $link); } }
function exportPDF() { global $i18n, $ClassDir; require_once 'fpdf/PDF_Chinese.php'; require_once $ClassDir . 'StringHelper.class.php'; $header = array("name" => "20", "gender" => "12", "birthday" => "25", "mobile" => "30", "phone" => "30", "office_phone" => "30", "fax" => "30", "addrees" => "20"); $pdf = new PDF_Chinese(); $pdf->AddGBFont('simsun', 'ËÎÌå'); $pdf->AddGBFont('simhei', 'ºÚÌå'); $pdf->AddGBFont('simkai', '¿¬Ìå_GB2312'); $pdf->AddGBFont('sinfang', '·ÂËÎ_GB2312'); $pdf->Open(); $pdf->SetMargins(5, 20, 5); $pdf->AddPage(); $pdf->SetAutoPageBreak("on", 20); $pdf->SetFont('simsun', '', 16); $pdf->Cell(0, 20, $i18n->_('Contact'), 0, 1, 'C'); foreach ($header as $title => $lenght) { $header_title_arr[] = $i18n->_(StringHelper::CamelCaseFromUnderscore($title)); } // Write header $pdf->SetFont('simsun', 'B', 7); $pdf->SetWidths(array_values($header)); $pdf->Row(array_values($header_title_arr)); // Write contact record $pdf->SetFont('simsun', '', 7); $apf_contact = DB_DataObject::factory('ApfContact'); $apf_contact->orderBy('id desc'); $apf_contact->find(); $i = 0; $page_rows = 30; while ($apf_contact->fetch()) { $i % $page_rows == 0 && $i ? $pdf->AddPage() : ""; $coloum_data = array(); foreach ($header as $title => $lenght) { $coloum_function = "get" . StringHelper::CamelCaseFromUnderscore($title); $coloum_data[] = $apf_contact->{$coloum_function}(); } $pdf->Row(array_values($coloum_data)); $i++; } $pdf->SetFont('Arial', '', 9); $pdf->AliasNbPages(); $filename = date("Y_m_d") . "_contact_export.pdf"; $pdf->Output($filename, true); exit; }