Creating a new PDF by Merging PDF documents using TCPDF

Merging two PDFs in PHP

What is PDF?

The Portable Document Format.

PDF stands for "Portable Document Format". Essentially, the format is used when you need to save files that cannot be modified but still need to be easily shared and printed. PDF is one of the most commonly used file types around.

PDFs generally contain shareable data, such as text or photo documents etc., and are accessible to anyone who has a computer or smartphone. Unlike a Word file, which requires Microsoft Word to view or edit, most browsers will open a PDF.

How to merge PDFs?

You can merge several documents into one. In PHP, you have a library called TCPDF which allows you to combine one or more PDF documents to one single document.

In this article, we will create a new PDF by Merging PDF documents using TCPDF.

Download the PDFMerger-tcpdf.zip here.

  1. Unzip it and copy it to the PHP server.
  2. Copy the PDF files into the same folder that you want to combine.
  3. Open the file: merge-pdf-example.php
  4. Provide PDF filenames and run it in the PHP server.
  5. TEST.pdf (output) is downloaded.

 

 

coding-zon-merge-pdf-tcpdf

 

merge-pdf-example.php

 
<?php 

include 'PDFMerger.php'; 
use PDFMerger\PDFMerger; 

 $pdf = new PDFMerger;

// 1 is pageNumber ,'1,2,3'  are pdf pagenumbers that you want to include in merge

$pdf->addPDF('pdf1.pdf', '1')     
    ->addPDF('pdf2.pdf', 'all')
    
    ->merge('download', 'TEST.pdf');

?>

Demo

 


 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Import excel file into mysql in PHP