12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace app\crm\controller;
  3. use think\Controller;
  4. use think\Request;
  5. class Preview extends Controller
  6. {
  7. public function previewPdf(Request $request)
  8. {
  9. # 处理跨域
  10. // header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
  11. // header('Access-Control-Allow-Credentials: true');
  12. // header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
  13. // header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, authKey, sessionId");
  14. # 相应类型
  15. header('Content-Type: application/pdf');
  16. header('Transfer-Encoding: chunked');
  17. $key = $request->param('key');
  18. $data = db('admin_printing_data')->field(['type', 'content'])->where('key', $key)->find();
  19. $contentArray = json_decode($data['content'], true);
  20. $content = $contentArray['data'];
  21. require_once(EXTEND_PATH.'tcpdf'.DS.'config'.DS.'tcpdf_config.php');
  22. require_once(EXTEND_PATH.'tcpdf'.DS.'tcpdf.php');
  23. $tcpdf = new \TCPDF();
  24. // 设置PDF页面边距:LEFT,TOP,RIGHT
  25. $tcpdf->SetMargins(10, 10, 10);
  26. // 设置字体,防止中文乱码
  27. $tcpdf->SetFont('simsun', '', 10);
  28. // 设置文件信息
  29. // $tcpdf->SetCreator(TITLE_NAME);
  30. // $tcpdf->SetAuthor(TITLE_NAME);
  31. $tcpdf->SetTitle("打印内容预览");
  32. // 删除预定义的打印 页眉/页尾
  33. $tcpdf->setPrintHeader(false);
  34. // 设置文档对齐,间距,字体,图片
  35. $tcpdf->SetCreator(PDF_CREATOR);
  36. $tcpdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  37. $tcpdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  38. // 自动分页
  39. $tcpdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  40. $tcpdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  41. $tcpdf->setFontSubsetting(true);
  42. $tcpdf->setPageMark();
  43. $tcpdf->AddPage();
  44. $html = $content;
  45. $tcpdf->writeHTML($html, true, false, true, true, '');
  46. $tcpdf->lastPage();
  47. $tcpdf->Output(ROOT_PATH.DS.'public'.DS.'temp'.DS.'pdf'.DS.'print.pdf','I');
  48. exit($this->fetch('preview', ['key' => $key]));
  49. }
  50. }