search
HomeBackend DevelopmentPHP ProblemHow to implement the PDF export function in PHP laravel

本篇文章给大家带来了关于PHP的相关知识,其中主要介绍了关于laravel实现导出PDF功能的相关问题,下面一起来看一下,希望对大家有帮助。

推荐学习:《PHP视频教程

一、laravel-tcpdf

导出PDF文件Laravel框架为我们集成了一个插件tcpdf。

下载地址:

https://github.com/elibyy/tcpdf-laravel

然后使用composer进行安装就可以了。

具体安装过程,请查看文末补充内容

使用的时候记得use 一下 命名空间。

但是这里有一个问题,使用这个插件导出文件无法使用中文,且我还没有找到解决办法,因此,这个laravel的tcpdf插件我就没有使用。

二、tcpdf

tcpdf官方网站:

tcpdf.org/

我下载了完整版的TCPDF

下载地址:https://github.com/tecnickcom/TCPDF.git

我们将下载的包放在框架根目录下的app/Extend/tcpdf中。

调用代码:

        require_once("../app/Extend/tcpdf/tcpdf.php");
$pdf = new TCPDF();
        // 设置文档信息
        $pdf->SetCreator('懒人开发网');
        $pdf->SetAuthor('懒人开发网');
        $pdf->SetTitle('TCPDF示例');
        $pdf->SetSubject('TCPDF示例');
        $pdf->SetKeywords('TCPDF, PDF, PHP');
 
        // 设置页眉和页脚信息
        $pdf->SetHeaderData('tcpdf_logo.jpg', 30, 'LanRenKaiFA.com', '学会偷懒,并懒出效率!', [0, 64, 255], [0, 64, 128]);
        $pdf->setFooterData([0, 64, 0], [0, 64, 128]);
 
        // 设置页眉和页脚字体
        $pdf->setHeaderFont(['stsongstdlight', '', '10']);
        $pdf->setFooterFont(['helvetica', '', '8']);
 
        // 设置默认等宽字体
        $pdf->SetDefaultMonospacedFont('courier');
 
        // 设置间距
        $pdf->SetMargins(15, 15, 15);//页面间隔
        $pdf->SetHeaderMargin(5);//页眉top间隔
        $pdf->SetFooterMargin(10);//页脚bottom间隔
 
        // 设置分页
        $pdf->SetAutoPageBreak(true, 25);
 
        // set default font subsetting mode
        $pdf->setFontSubsetting(true);
 
        //设置字体 stsongstdlight支持中文
        $pdf->SetFont('stsongstdlight', '', 14);
 
        //第一页
        $pdf->AddPage();
        $pdf->writeHTML(&#39;<p style="text-align: center"><h1 id="第一页内容">第一页内容</h1></p>&#39;);
        $pdf->writeHTML(&#39;<p>我是第一行内容</p>&#39;);
        $pdf->writeHTML(&#39;<p style="color: red">我是第二行内容</p>&#39;);
        $pdf->writeHTML(&#39;<p>我是第三行内容</p>&#39;);
        $pdf->Ln(5);//换行符
        $pdf->writeHTML(&#39;<p><a href="http://www.lanrenkaifa.com/" rel="external nofollow"  title="">懒人开发网</a></p>&#39;);
 
        //第二页
        $pdf->AddPage();
        $pdf->writeHTML(&#39;<h1 id="第二页内容">第二页内容</h1>&#39;);
 
        //输出PDF
        $pdf->Output(&#39;t.pdf&#39;, &#39;I&#39;);//I输出、D下载

三、TCPDF解决保存中文文件名的方法

这部分是百度过来的,网上挺多关于这个的文章的,内容基本一致。

1:找到output函数,注释以下代码(在7560行左右):

if ($dest[0] != &#39;F&#39;) {
    $name = preg_replace(&#39;/[s]+/&#39;, &#39;_&#39;, $name);
    $name = preg_replace(&#39;/[^a-zA-Z0-9_.-]/&#39;, &#39;&#39;, $name);
}

2:搜索下面这行代码

header(&#39;Content-Disposition: attachment; filename="&#39;.basename($name).&#39;"&#39;);

替换成:

header(&#39;Content-Disposition: attachment; filename="&#39;.$name.&#39;"&#39;);

以上大概就是tcpdf的基本使用。

补充

laravel5.8引入第三方类库的方法详解

有需求需要使用PHPMailer发送邮件。

那么首先需要引入PHPMailer这个第三方的类库。我是这样做的:

1:在app目录下新建Extend目录。如下图所示:

将PHPMailer放入Extend目录下。如下图所示

2:修改项目根目录下的composer.json文件

"autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories",
            "app/Extend/PHPMailer/src"
        ]
    },

添加你第三方类库的位置到autoload中

3:执行composer命令,在网站根目录下:

composer dump-autoload

4:调用:

(1):使用命名空间

use PHPMailer\src\PHPMailer;

(2):调用

 //实例化PHPMailer核心类
$mail = new PHPMailer();

如果报错,就在实例化前边加一个转义符\

至此,laravel引入第三方类库成功。

推荐学习:《PHP视频教程

The above is the detailed content of How to implement the PDF export function in PHP laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:脚本之家. If there is any infringement, please contact admin@php.cn delete
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft