个性化阅读
专注于IT技术分析

如何在Laravel中合并多个PDF

本文概述

愿意将有关不同部门的所有工程图, 报告, 信息发送给你的老板, 但它们都是包含相关信息的PDF文件吗?许多用户只是简单地合并PDF而已。如今, 将文件合并为通用PDF极为有用且必要, 因此, 如果你为某些企业提供某种管理应用程序, 则将此类功能添加到系统中可能会很有用, 因此他们无需安装外部程序或在线进行操作使用第三方服务。

在本文中, 我们将向你展示如何使用PDFMerger库将多个PDF合并为一个。

1.安装pdfmerger

PDFMerger依靠Setasign的fpdf和fpdi类(作为依赖项自动安装)。原始库还有其他版本, 因为原始库托管在Codeplex中, 因此已移植到PHP5中, 但是它们没有最低的稳定性级别(它们可以工作, 但不适用于作曲家)。与composer一起使用的库派生器是@rguedes创建的一个库派生器, 至于Laravel, 要使用的任何依赖项都需要最低的稳定性级别(不是dev-master)。要在Laravel项目中继续安装软件包, 请打开终端, 切换到项目目录, 然后使用composer安装库:

composer require rguedes/pdfmerger

安装之后, 你将能够使用PDFMerger类及其方法。有关此库的fork的更多信息, 请访问Github上的存储库。

2.使用库

该库的用法非常简单明了, 你可以创建PDF合并的实例。此类允许你使用addPDF方法合并多个文件, 并最终使用merge方法生成合并结果。你可以选择将哪些PDF页面添加到最终页面中, 并决定如何以及在何处生成最终PDF。

指定要合并的页面

PDFMerger类允许你使用addPDF方法根据需要合并多个PDF, 你只需要提供路径作为第一个参数, 并使用第二个参数指示文件的哪些页面应合并到最终文件中即可。串。例如, 你可以合并PDF的所有页面:

$pdf = new PDFMerger();

// Add all the pages of the PDF to merge 
$pdf->addPDF("somePdfToMerge.pdf", 'all');

通过提供用逗号分隔的编号来具体指定所需的页面:

$pdf = new PDFMerger();

// Add only the pages 1, 3 and 5
$pdf->addPDF("somePdfToMerge.pdf", '1, 3, 5');

或使用范围, 例如从第5页到第10页:

$pdf = new PDFMerger();

// Add only the pages from 5 to 10
$pdf->addPDF("somePdfToMerge.pdf", '5-10');

合并模型

使用merge方法, 你将生成一个PDF, 其中包含添加的文件(每个PDF的指定页面)。该文件可以根据你的需要进行存储或作为响应返回。需要将处理PDF的方法作为方法合并的第一个参数提供, 该参数的可能值为:浏览器, 下载, 字符串或文件。作为第二个参数, 将保存PDF的路径(如果使用文件作为方法)或将用于返回PDF的PDF名称(使用浏览器或下载):

产生直接下载

如果你不需要将PDF保存在任何地方, 而只是将其生成并作为响应返回, 则可以使用下载标识符强制直接下载生成的PDF:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

// Require PDF class of the library
use PDFMerger;

class DefaultController extends Controller
{
    /**
    * Index route
    *
    * @return Response
    */
    public function index()
    {
        // Absolute path of the PDFs to merge
        // In this example we have them inside the /public folder
        // of the project
        $pdfFile1Path = public_path() . '/file1.pdf';
        $pdfFile2Path = public_path() . '/file2.pdf';
        $pdfFile3Path = public_path() . '/file3.pdf';

        // Create an instance of PDFMerger
        $pdf = new PDFMerger();

        // Add 2 PDFs to the final PDF
        $pdf->addPDF($pdfFile1Path, 'all');
        $pdf->addPDF($pdfFile3Path, '1, 2, 3');

        // Generate download of "mergedpdf.pdf"
        $pdf->merge('download', "mergedpdf.pdf");
    }
}

保存到文件中

你可以将合并的结果存储到服务器中的文件中:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

// Require PDF class of the library
use PDFMerger;

class DefaultController extends Controller
{
    /**
    * Index route
    *
    * @return Response
    */
    public function index()
    {
        // Absolute path of the PDFs to merge
        // In this example we have them inside the /public folder
        // of the project
        $pdfFile1Path = public_path() . '/file1.pdf';
        $pdfFile2Path = public_path() . '/file2.pdf';
        $pdfFile3Path = public_path() . '/file3.pdf';

        // Create an instance of PDFMerger
        $pdf = new PDFMerger();
        
        // Add 2 PDFs to the final PDF
        $pdf->addPDF($pdfFile1Path, 'all');
        $pdf->addPDF($pdfFile2Path, 'all');

        // Merge the files into a file in some directory
        $pathForTheMergedPdf = public_path() . "/result.pdf";

        // Merge PDFs into a file
        $pdf->merge('file', $pathForTheMergedPdf);

        // Do something else here, as return
        // a response from the controller ...
    }
}

检索结果pdf的二进制内容

如果你需要检索生成的文件(二进制数据)的内容而不将其保存在任何地方, 则可以使用字符串输出方法将内容作为变量检索。在这种情况下, 我们将返回二进制内容作为带有PDF标头的响应(以在浏览器中查看)。你也可以根据需要使用浏览器类型:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

// Require PDF class of the library
use PDFMerger;

class DefaultController extends Controller
{
    /**
    * Index route
    *
    * @return Response
    */
    public function index()
    {
        // Absolute path of the PDFs to merge
        // In this example we have them inside the /public folder
        // of the project
        $pdfFile1Path = public_path() . '/file1.pdf';
        $pdfFile2Path = public_path() . '/file2.pdf';
        $pdfFile3Path = public_path() . '/file3.pdf';

        // Create an instance of PDFMerger
        $pdf = new PDFMerger();
        
        // Add 2 PDFs to the final PDF
        $pdf->addPDF($pdfFile1Path, 'all');
        $pdf->addPDF($pdfFile2Path, 'all');

        // Merge the files and retrieve its PDF binary content
        $binaryContent = $pdf->merge('string', "mergedpdf.pdf");

        // Return binary content as response
        return response($binaryContent)
            ->header('Content-type' , 'application/pdf')
        ;
    }
}

编码愉快!

赞(0)
未经允许不得转载:srcmini » 如何在Laravel中合并多个PDF

评论 抢沙发

评论前必须登录!