安装
composer require tecnickcom/tcpdf
示例代码
try {
$order = requestAPI(env('A2_URL') . 'akinst/order/detail', ['user_id' => Auth::id(), 'order_number' => $request->get('p')]);
$data = Arr::get($order,'0',[]);
$data['total_amount_ch'] = convertAmountToCn(Arr::get($data,'total_amount',0));
$data['downdate'] = date('Y-m-d',time()).'星期'.mb_substr( "日一二三四五六",date("w" ,time()),1,"utf-8" );;
$view = view('tcpdf/contract', compact('data'));
$content = response($view)->getContent();
$pdf = new \TCPDF('p', 'mm', 'A4', true, 'UTF-8', false);
$pdf->SetFont('stsongstdlight', '', 10);
$pdf->SetCreator('创作者');
$pdf->SetAuthor('作者');
$pdf->SetTitle('网络销售合同');
$pdf->SetSubject('网络销售合同');
$pdf->SetKeywords('网络销售合同');
$pdf->SetHeaderData('', 63, '', '头部' , array(0, 0, 0), array(0, 0, 0));
$pdf->setHeaderFont(Array('stsongstdlight', '', '10'));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetMargins(15, 20, 15);
$pdf->SetAutoPageBreak(TRUE, 0);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setPrintHeader(true);
$pdf->setPrintFooter(false);
$pdf->AddPage();
$html = $content;
$pdf->setPageMark();
$pdf->SetXY(15, 20);
$pdf->writeHTML($html, true, false, true, false, '');
$tmpPath = "pdf/" . date("Ymd");
if (!Storage::disk('public')->exists($tmpPath)) {
Storage::disk('public')->makeDirectory($tmpPath);
}
$filename = date("YmdHis") . "_" . rand(0, 10000) . '.pdf';
$filepath = 'storage/' . $tmpPath . "/" . $filename;
for ($i = 1; $i <= $pdf->getNumPages(); $i++) {
$pdf->setPage($i);
$pdf->Image(public_path('/mallWeb/images/official.png'), 140, 220, 50, 43, '', '', '', false, 168, '', false, false, false, false, false, false);
}
$pdf->Output(public_path($filepath), 'F');
if (file_exists(public_path($filepath))) {
$retval['code'] = 0;
$retval['filename'] = url($filepath);
$retval['msg'] = '恭喜,生成合同的PDF文件成功.';
return $retval;
} else {
$retval['code'] = 1;
$retval['msg'] = '生成合同的PDF文件失败,请联系系统管理员.';
return $retval;
}
} catch (\Exception $e) {
return $e;
}
|