Bladeren bron

增加PDF导出

lzj500 3 jaren geleden
bovenliggende
commit
a6e7eb3eb6

+ 2 - 2
thinkphp6/app/index/controller/Base.php

@@ -24,7 +24,7 @@ class Base extends \app\common\controller\Base
 {
     private $apikey = "32a1ff74699ff2d6ce4c497cb94cb5c8";//密钥
 
-
+    protected $checkSignOpen = true;//是否校验签名,true校验,false不校验
     /**
      * 构造函数
      * Base constructor.
@@ -32,7 +32,7 @@ class Base extends \app\common\controller\Base
     public function __construct()
     {
         parent::__construct();//继承父类构造函数
-        $this->checkSignOpen=true;
+
         if ($this->checkSignOpen) {
            $checkRes=$this->checkApiSign();
            if($checkRes['code']!==0){

+ 89 - 0
thinkphp6/app/index/controller/Pdf.php

@@ -0,0 +1,89 @@
+<?php
+
+
+namespace app\index\controller;
+
+
+
+
+use think\facade\View;
+
+class Pdf extends Base
+{
+    /*
+     * 文档:https://tcpdf.org/examples/
+     * composer 安装命令:composer require tecnickcom/tcpdf
+     *
+     * */
+    /**
+     * @var bool
+     */
+    protected $checkSignOpen = false;//是否校验签名,true校验,false不校验
+
+    public function htmlTest1(){
+        View::assign("title","这是标是,从后台来的");
+        return view();
+    }
+
+    public function createPdfTest1(){
+//        echo "test1 begin";
+
+        $pdf=new \TCPDF("P","mm","A4");
+        // 设置文档信息
+        $pdf->SetCreator('Helloweba');
+        $pdf->SetAuthor('yueguangguang');
+        $pdf->SetTitle('Welcome to helloweba.com!');
+        $pdf->SetSubject('TCPDF Tutorial');
+        $pdf->SetKeywords('TCPDF, PDF, PHP');
+
+// 设置页眉和页脚信息
+        $pdf->SetHeaderData('logo.png', 30, 'Helloweba.com', '致力于WEB前端技术在中国的应用',
+            array(0,64,255), array(0,64,128));
+        $pdf->setFooterData(array(0,64,0), array(0,64,128));
+
+// 设置页眉和页脚字体
+        $pdf->setHeaderFont(Array('stsongstdlight', '', '10'));
+        $pdf->setFooterFont(Array('helvetica', '', '8'));
+
+// 设置默认等宽字体
+        $pdf->SetDefaultMonospacedFont('courier');
+
+// 设置间距
+        $pdf->SetMargins(15, 27, 15);
+        $pdf->SetHeaderMargin(5);
+        $pdf->SetFooterMargin(10);
+
+// 设置分页
+        $pdf->SetAutoPageBreak(TRUE, 25);
+
+// set image scale factor
+        $pdf->setImageScale(1.25);
+
+// set default font subsetting mode
+        $pdf->setFontSubsetting(true);
+
+//设置字体
+        $pdf->SetFont('stsongstdlight', '', 14);
+
+        $pdf->AddPage();
+
+        $str1 = '<h1>这是h1</h1><div style="color: #f00;font-size: 14px;">这是红色</div><div style="color: #ddd;font-size: 20px;">这是灰色</div>';
+        $pdf->writeHTML($str1, true, false, true, false, '');
+        $pdf->AddPage();
+
+        $str1 = '<h1>这是第2页</h1><div style="color: #f00;font-size: 14px;">这是红色</div><div style="color: #ddd;font-size: 20px;">这是灰色</div>';
+        $pdf->writeHTML($str1, true, false, true, false, '');
+
+        $pdf->AddPage();
+        View::assign("title","这是标是,从后台来的");
+        $str1=View::fetch("html_test1");
+        $pdf->writeHTML($str1, true, false, true, false, '');
+        //输出PDF
+        $path=public_path()."/storage/pdf/".date("YmdHis").".pdf";
+        $pdf->Output($path, 'F');
+        $this->success("","成功");
+    }
+
+
+
+}

+ 3 - 1
thinkphp6/composer.json

@@ -24,7 +24,9 @@
         "topthink/framework": "^6.0.0",
         "topthink/think-orm": "^2.0",
         "topthink/think-multi-app": "^1.0",
-        "phpoffice/phpspreadsheet": "^1.18"
+        "phpoffice/phpspreadsheet": "^1.18",
+        "tecnickcom/tcpdf": "^6.4",
+        "topthink/think-view": "^1.0"
     },
     "require-dev": {
         "symfony/var-dumper": "^4.2",

+ 155 - 1
thinkphp6/composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "35d09aa8e1e0710404eb10be0742fece",
+    "content-hash": "c77cf8360f473eddbf9d33eef75ca58c",
     "packages": [
         {
             "name": "ezyang/htmlpurifier",
@@ -1207,6 +1207,78 @@
             "time": "2021-01-22T09:19:47+00:00"
         },
         {
+            "name": "tecnickcom/tcpdf",
+            "version": "6.4.4",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/tecnickcom/TCPDF.git",
+                "reference": "42cd0f9786af7e5db4fcedaa66f717b0d0032320"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/tecnickcom/TCPDF/zipball/42cd0f9786af7e5db4fcedaa66f717b0d0032320",
+                "reference": "42cd0f9786af7e5db4fcedaa66f717b0d0032320",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.3.0"
+            },
+            "type": "library",
+            "autoload": {
+                "classmap": [
+                    "config",
+                    "include",
+                    "tcpdf.php",
+                    "tcpdf_parser.php",
+                    "tcpdf_import.php",
+                    "tcpdf_barcodes_1d.php",
+                    "tcpdf_barcodes_2d.php",
+                    "include/tcpdf_colors.php",
+                    "include/tcpdf_filters.php",
+                    "include/tcpdf_font_data.php",
+                    "include/tcpdf_fonts.php",
+                    "include/tcpdf_images.php",
+                    "include/tcpdf_static.php",
+                    "include/barcodes/datamatrix.php",
+                    "include/barcodes/pdf417.php",
+                    "include/barcodes/qrcode.php"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "LGPL-3.0-only"
+            ],
+            "authors": [
+                {
+                    "name": "Nicola Asuni",
+                    "email": "info@tecnick.com",
+                    "role": "lead"
+                }
+            ],
+            "description": "TCPDF is a PHP class for generating PDF documents and barcodes.",
+            "homepage": "http://www.tcpdf.org/",
+            "keywords": [
+                "PDFD32000-2008",
+                "TCPDF",
+                "barcodes",
+                "datamatrix",
+                "pdf",
+                "pdf417",
+                "qrcode"
+            ],
+            "support": {
+                "issues": "https://github.com/tecnickcom/TCPDF/issues",
+                "source": "https://github.com/tecnickcom/TCPDF/tree/6.4.4"
+            },
+            "funding": [
+                {
+                    "url": "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&currency_code=GBP&business=paypal@tecnick.com&item_name=donation%20for%20tcpdf%20project",
+                    "type": "custom"
+                }
+            ],
+            "time": "2021-12-31T08:39:24+00:00"
+        },
+        {
             "name": "topthink/framework",
             "version": "v6.0.7",
             "source": {
@@ -1440,6 +1512,88 @@
                 "source": "https://github.com/top-think/think-orm/tree/v2.0.39"
             },
             "time": "2021-02-26T10:20:00+00:00"
+        },
+        {
+            "name": "topthink/think-template",
+            "version": "v2.0.8",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/top-think/think-template.git",
+                "reference": "abfc293f74f9ef5127b5c416310a01fe42e59368"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/top-think/think-template/zipball/abfc293f74f9ef5127b5c416310a01fe42e59368",
+                "reference": "abfc293f74f9ef5127b5c416310a01fe42e59368",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=7.1.0",
+                "psr/simple-cache": "^1.0"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "think\\": "src"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "Apache-2.0"
+            ],
+            "authors": [
+                {
+                    "name": "liu21st",
+                    "email": "liu21st@gmail.com"
+                }
+            ],
+            "description": "the php template engine",
+            "support": {
+                "issues": "https://github.com/top-think/think-template/issues",
+                "source": "https://github.com/top-think/think-template/tree/v2.0.8"
+            },
+            "time": "2020-12-10T07:52:03+00:00"
+        },
+        {
+            "name": "topthink/think-view",
+            "version": "v1.0.14",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/top-think/think-view.git",
+                "reference": "edce0ae2c9551ab65f9e94a222604b0dead3576d"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/top-think/think-view/zipball/edce0ae2c9551ab65f9e94a222604b0dead3576d",
+                "reference": "edce0ae2c9551ab65f9e94a222604b0dead3576d",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=7.1.0",
+                "topthink/think-template": "^2.0"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "think\\view\\driver\\": "src"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "Apache-2.0"
+            ],
+            "authors": [
+                {
+                    "name": "liu21st",
+                    "email": "liu21st@gmail.com"
+                }
+            ],
+            "description": "thinkphp template driver",
+            "support": {
+                "issues": "https://github.com/top-think/think-view/issues",
+                "source": "https://github.com/top-think/think-view/tree/v1.0.14"
+            },
+            "time": "2019-11-06T11:40:13+00:00"
         }
     ],
     "packages-dev": [

+ 2 - 0
thinkphp6/public/storage/.gitignore

@@ -0,0 +1,2 @@
+*
+!.gitignore

+ 69 - 0
thinkphp6/view/index/pdf/html_test1.html

@@ -0,0 +1,69 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>测试模板</title>
+    <style>
+        * {
+            padding: 0;
+            margin: 0;
+        }
+
+        .main {
+            width: 100%;
+        }
+
+        .box1 {
+            background-color: #ff0000;
+            color: #fff;
+            width: 100px;
+            padding: 20px;
+            font-size: 12px;
+        }
+
+        .box2 {
+            background-color: #3c78d8;
+            color: #fff;
+            width: 50%;
+            padding: 10px;
+            font-size: 16px;
+        }
+        .box3{
+            /*padding: 20px;*/
+        }
+        .table1 {
+            width: 100%;
+            text-align: center;
+            border: solid #eee 1px;
+            border-collapse: collapse;
+        }
+
+        .table1 td {
+
+            border: solid #eee 1px;
+            border-collapse: collapse;
+        }
+    </style>
+</head>
+<body>
+<div class="main">
+    <h1>{$title}</h1>
+    <div class="box1">红色底,白色字</div>
+    <div class="box2">蓝色底,白色字</div>
+    <div class="box3">
+        <table class="table1">
+            <tr>
+                <td>列1</td>
+                <td>列2</td>
+                <td>列3</td>
+            </tr>
+            <tr>
+                <td>列1</td>
+                <td>列2</td>
+                <td>列3</td>
+            </tr>
+        </table>
+    </div>
+</div>
+</body>
+</html>