/** * 导出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"); }
<?php include_once "api.php"; include_once "fpdf_ch.php"; //print_r($_REQUEST); $strAddress = isset($_POST['address']) ? trim($_REQUEST['address']) : ""; $strComment = isset($_POST['comment']) ? trim($_REQUEST['comment']) : ""; $strDate = isset($_POST['date']) ? trim($_REQUEST['date']) : date("Y m d"); $iExpress = isset($_REQUEST['express']) ? intval($_REQUEST['express']) : 0; $arrInfo = parseAddress($strAddress); $arrPos = array_key_exists($iExpress, $arrPosition) ? $arrPosition[$iExpress] : $arrPosition[0]; $objPDF = new PDF_Chinese("Landscape", "mm", $arrPos['size']->toArray()); $objPDF->AddGBFont(); $objPDF->SetFontSize(11); $objPDF->AddPage(); $objPDF->SetFont("Arial"); $objPDF->Text($arrPos['sendDate']->x, $arrPos['sendDate']->y, parseDate($strDate, $arrPos['dateFormat'])); $objPDF->Text($arrPos['srcPhone']->x, $arrPos['srcPhone']->y, "0571-85790698"); if ($arrPos['srcPCode'] != NULL) { $objPDF->Text($arrPos['srcPCode']->x, $arrPos['srcPCode']->y, "310053"); } $objPDF->Text($arrPos['dstPhone1']->x, $arrPos['dstPhone1']->y, $arrInfo['phone1']); if (isset($arrInfo['phone2'])) { $objPDF->Text($arrPos['dstPhone2']->x, $arrPos['dstPhone2']->y, $arrInfo['phone2']); } if ($arrPos['dstPCode'] != NULL) { $objPDF->Text($arrPos['dstPCode']->x, $arrPos['srcPCode']->y, $arrInfo['postcode']); } $objPDF->SetFont("GB"); $oSize = $objPDF->GetFontSize(); if ($arrPos['srcAddrSize'] != NULL) {
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; }