How 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('<p style="text-align: center"><h1>第一页内容</h1></p>'); $pdf->writeHTML('<p>我是第一行内容</p>'); $pdf->writeHTML('<p style="color: red">我是第二行内容</p>'); $pdf->writeHTML('<p>我是第三行内容</p>'); $pdf->Ln(5);//换行符 $pdf->writeHTML('<p><a href="http://www.lanrenkaifa.com/" rel="external nofollow" title="">懒人开发网</a></p>'); //第二页 $pdf->AddPage(); $pdf->writeHTML('<h1>第二页内容</h1>'); //输出PDF $pdf->Output('t.pdf', 'I');//I输出、D下载
三、TCPDF解决保存中文文件名的方法
这部分是百度过来的,网上挺多关于这个的文章的,内容基本一致。
1:找到output函数,注释以下代码(在7560行左右):
if ($dest[0] != 'F') { $name = preg_replace('/[s]+/', '_', $name); $name = preg_replace('/[^a-zA-Z0-9_.-]/', '', $name); }
2:搜索下面这行代码
header('Content-Disposition: attachment; filename="'.basename($name).'"');
替换成:
header('Content-Disposition: attachment; filename="'.$name.'"');
以上大概就是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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
