使用tcpdf軟體 線上預覽pdf檔案
require_once('/var/www/html/htdocs/tcpdf/config/lang/eng.php');//包含兩個檔案就載入tcpdf庫
require_once('/var/www/html/htdocs/tcpdf/tcpdf.php');
require_once('/var/www/html/htdocs/database.php');
$thbgcolor ="bgcolor=\"#C9D6A6\"";//定義變數
$tdbgcolor0="bgcolor=\"#DFE3E6\"";
$tdbgcolor1="bgcolor=\"#EDF1F4\"";
$tdbgcolor2="bgcolor=\"#66CCFF\"";
$tdbgcolor3="bgcolor=\"#B0C4DE\"";
$tdbgcolor4="bgcolor=\"#FFE4C4\"";
define("MSSP_TABDEF", "border=\"1\" cellpadding=\"1\" cellspacing=\"1\"");//定義常量
define("MSSP_TABDEF2", " border=\"1\" cellpadding=\"1\" cellspacing=\"1\" width=\"%50\"");
define("MSSP_TDWIDTH", " width=\"76\" ");
define("MSSP_TDWIDTH2", " width=\"65\" ");
define("MSSP_TDWIDTH3", " width=\"68\" ");
define("MSSP_TDWIDTH4", " width=\"76\" ");
define("MSSP_TDWIDTH5", " width=\"78\" ");
$Product_info_total = 0;
$Contract_ID= $_REQUEST['Contract_ID'];//接收資料
$pdf_filename=$Contract_ID;
header("Content-Disposition: attachment; filename=$pdf_filename.pdf");
$sql = "select c.no Contract_No, c.Date Contract_Date, e.Name Employee_Name from tbl_contract c, tbl_employee e where c.id=65 and e.id=Employee_ID";
//從資料庫查出合同為65的合同名稱 合同日期和銷售
display_content_pdf($Contract_ID,$msspdb,$pdf_filename);//header("/htdocs/download/" . $pdf_filename);
function display_content_pdf($Contract_ID,$msspdb,$pdf_filename)
{
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, '30', '','*****有限公司');//頭內容想查詢常量,需到/tcpdf/config/tcpdf_config.php中查詢
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
//$pdf->setLanguageArray($l);//以上為固定的一些樣式,可以改變,tcpdf_config.php檔案更改常量
// ---------------------------------------------------------
// set font
$pdf->AddPage('Landscape','A4'); // 橫向列印
$pdf->SetFont('stsongstdlight', '', 12);//stsongstdlight是字型,這種字型很小,可以使用dejavusans是utf - 8的Unicode字型,如果你只需要列印標準ASCII字元,您可以使用核心字型像。helvetica或次減少檔案大小。
$html = '<h2>***有限公司—用印申請單</h2>';
$pdf->writeHTML($html, true, false, true, false, '');// writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='')
$html = display_contract_content_pdf($pdf, $Contract_ID,$msspdb);
// Dest:設定PDF輸出的方式。I,預設值,在瀏覽器中開啟;D,點選下載按鈕,PDF檔案會被下載下來;
// F,檔案會被儲存在伺服器中;S,PDF會以字串形式輸出;E:PDF以郵件的附件輸出。
$pdf->Output("contract_" . $pdf_filename . ".pdf","I");
}
function display_contract_content_pdf($pdf, $Contract_ID,$msspdb)
{
global $thbgcolor ;
global $tdbgcolor0;
global $tdbgcolor1;
global $tdbgcolor2;
$sql = "
select
c.no Contract_No, c.Date Contract_Date, c.Customer Contract_Customer, c.Verb_Date Contract_Verb_Date, Payment_Method,
Net_Cost,Tax_Rate,Date_Clause,Sales_Date,
Service_Cost,Capital_cost,Assessment_cost,Total_cost,Gross_Sales,Gross_Profit,Gross_margin,
Business_Date,Business_Name,Department_Date,Department_Name Department_Manager_Name,Executive_Date,Executivet_Name,
e.Name Employee_Name,
d.Name Department_Name
from tbl_contract c, tbl_employee e, tbl_department d
where
c.id=$Contract_ID
and e.id=Employee_ID
and e.Department_ID = d.ID
";
$maxrows = 10;
$result = mysql_query($sql,$msspdb) or die('fail:'.mysql_error());
while ($rs = mysql_fetch_array($result))
{
$pdf->AddPage('Landscape','A4'); // 橫向列印
$pdf->SetFont('stsongstdlight', 'B', 18);
$pdf->Cell(0, 1, '****合同—用印申請單', 0, 1, 'C');
$pdf->SetFont('stsongstdlight', '', 7);
$thtml = '<br><br><br><table '.MSSP_TABDEF2 . '>';
$html = sprintf("<tr> <td %s align=\"right\">日期:</td><td>%s</td>", $tdbgcolor0, $rs['Contract_Date']);
$thtml .= $html;
$html = sprintf(" <td %s align=\"right\">合同編號:</td><td>%s</td>", $tdbgcolor0, $rs['Contract_No']);
$thtml .= $html;
$thtml .= "</tr> ";
$html = sprintf("<tr> <td %s align=\"right\">銷售負責人:</td><td>%s</td>", $tdbgcolor0, $rs['Employee_Name']);
$thtml .= $html;
$html = sprintf(" <td %s align=\"right\">客戶名稱:</td><td>%s</td>", $tdbgcolor0, $rs['Contract_Customer']);
$thtml .= $html;
$thtml .= "</tr> ";
$html = sprintf("<tr> <td %s align=\"right\">部門:</td><td>%s</td>", $tdbgcolor0, $rs['Department_Name']);
$thtml .= $html;
$html = sprintf(" <td %s align=\"right\">合同簽訂日期:</td><td>%s</td>", $thbgcolor, $rs['Contract_Verb_Date']);
$thtml .= $html;
$thtml .= "</tr> ";
$thtml .= "</table>";
$html = sprintf("<tr> <td %s align=\"center\">合同產品資訊表(銷售人員填寫)</td>", $tdbgcolor0);
$thtml .= $html;
$html = sprintf(" <td %s align=\"center\">考核成本資訊表(產品經理填寫)</td>", $tdbgcolor2);
$thtml .= $html;
$thtml .= "</tr>";
$thtml .= "<tr>";
$thtml .= '<td>';
$thtml .= display_contract_product_info_pdf($Contract_ID,$msspdb,$maxrows);//呼叫方法
$thtml .= "</td> ";
$thtml .= '<td>';
$thtml .= display_contract_assessment_info_pdf($Contract_ID,$msspdb,$maxrows);//呼叫方法
$thtml .= "</td> ";
$thtml .= "</tr> ";
$thtml .= "<tr>";
$thtml .= '<td>';
$thtml .= display_contract_clause_info_pdf($Contract_ID,$msspdb,4,$rs['Payment_Method'],
$rs['Net_Cost'],$rs['Tax_Rate'],$rs['Date_Clause'],$rs['Employee_Name'],$rs['Sales_Date']);//呼叫方法
$thtml .= "</td> ";
$thtml .= '<td>';
$thtml .= display_contract_other_info_pdf($Contract_ID,$msspdb,3,$rs['Service_Cost'],$rs['Capital_cost'],
$rs['Assessment_cost'],$rs['Total_cost'],$rs['Gross_Sales'],$rs['Gross_Profit'],$rs['Gross_margin'],
$rs['Business_Date'],$rs['Business_Name'],
$rs['Department_Date'],$rs['Department_Manager_Name'],
$rs['Executive_Date'],$rs['Executivet_Name']);//呼叫方法
$thtml .= "</td> ";
$thtml .= "</tr> ";
$thtml .= "</table>";
$pdf->writeHTML($thtml, true, false, true, false, '');
}
return $thtml;
}
?>