123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. $content = str_replace('\n', '', $content);
  22. $content = str_replace('\\', '', $content);
  23. require_once(EXTEND_PATH.'tcpdf'.DS.'config'.DS.'tcpdf_config.php');
  24. require_once(EXTEND_PATH.'tcpdf'.DS.'tcpdf.php');
  25. $tcpdf = new \TCPDF();
  26. // 设置PDF页面边距:LEFT,TOP,RIGHT
  27. $tcpdf->SetMargins(10, 10, 10);
  28. // 设置默认等宽字体
  29. $tcpdf->SetDefaultMonospacedFont('courier');
  30. // 设置字体,防止中文乱码
  31. $tcpdf->SetFont('simsun', '', 10);
  32. // 设置文件信息
  33. // $tcpdf->SetCreator(TITLE_NAME);
  34. // $tcpdf->SetAuthor(TITLE_NAME);
  35. $tcpdf->SetTitle("打印内容预览");
  36. // 删除预定义的打印 页眉/页尾
  37. $tcpdf->setPrintHeader(false);
  38. // 设置文档对齐,间距,字体,图片
  39. $tcpdf->SetCreator(PDF_CREATOR);
  40. $tcpdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  41. $tcpdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  42. // 自动分页
  43. $tcpdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  44. $tcpdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  45. $tcpdf->setFontSubsetting(true);
  46. $tcpdf->setPageMark();
  47. $tcpdf->AddPage();
  48. $html = $content;
  49. $tcpdf->writeHTML($html, true, false, true, true, '');
  50. $tcpdf->lastPage();
  51. $tcpdf->Output(ROOT_PATH.DS.'public'.DS.'temp'.DS.'pdf'.DS.'print.pdf','I');
  52. exit($this->fetch('preview', ['key' => $key]));
  53. }
  54. }