How to convert Excel to PDF

You can convert an Excel file to PDF in 3 different ways. First, you can use the inbuilt Excel function. But this option is limited only to users with the Excel Application. Secondly, you can use an online PDF converter using any device, regardless of whether you have Excel software or not. Lastly, If you are a developer, you can convert a PDF file to Excel using the PHP free library called PHPspreadsheet. This article will look at the 3 methods of converting an Excel file to PDF format.

What is a PDF?

PDF files are text documents that use the Portable Document Format. PDF files are often used to distribute documents electronically. They are easy to print and can be opened with various software programs.

Advantages of Converting Excel to PDF

1. PDF is a more versatile format than Excel.

PDF format is more versatile than Excel format because it can be used to save different types of documents, including documents that have graphics and diagrams.

2. PDF can be edited and modified more easily than Excel.

This is because PDF files are not always formatted in a consistent manner. This means that you can easily change a PDF file's text, images, and layout.

3. PDF can be shared and distributed more easily than Excel.

PDF is a memory-saving format that allows users to distribute the files easily

4. PDF can be opened in various software applications, including Microsoft Office.

You don't need specific software or device to open a PDF file.

5. PDF is a closed format.

This means that once a PDF document is created, it is difficult to change or update it. This is a security feature because it prevents unauthorized access to the data contained in the document.

How to convert Excel to PDF Using Excel application

1. Click on the file

2. Click on Save AS

3. Click on the drop-down arrow

4. Select save as *PDF

How to convert a specific spreadsheet or entire workbook into a PDF

  • Click on options

  • select page range or tick the entire worksheet

5. Click Save

How to convert Excel to PDF Using online tools

You can still convert your document to PDF if you do not have an Excel application. Online tools make work easy for users to convert excel to PDF and other formats. There are several online tools that you can use for free and without using your Email. For example, Knightsheets.com Excel to PDF converter. Follow these steps;

How to Convert Excel to PDF Using PHP

PHPspreadsheet is an open-source PHP library for reading and writing Excel spreadsheets. You can use it alongside a PDF library to convert Excel into PDF. Various PDF libraries are compatible with PHP spreadsheets, they include DOMPdf, MPdf, and TCPDF.

Prerequisites

  • PHPspreadsheet Library
  • Server (Xamp, Wamp, Lamp, Mamp, etc)
  • DOMPdf / MPdf/ TCPDF/ Library
  • Composer (Install it first before starting the project)

1. Create a folder inside the htdocs folder. Give it a name to distinguish your project. (In this example, I will name mine exceltopdf)

2. Open CMD and navigate to the folder you have just created

3. Run the command to install PHPspreadsheet (composer require phpoffice/phpspreadsheet)

4. Run the command to install a PDF library. In this example, I will use MPdf (composer require mpdf/mpdf)

5. Create a folder named files to act as the upload directory

6. Create an index file (Index.php) and open it using any code editor and Paste the code below. The index page will consist of an upload form

<html> 
<body>
<?php <form action="upload.php" enctype="multipart/form-data" method="POST">
<input id="fileToUpload" name="fileToUpload" required="" type="file" value="Select File" >
<button class="btn" name="upload">Upload</button> 
?> 
</body>
</html>

6. Create an upload.php file and paste the following code

<?php
require 'vendor/autoload.php';
/* code by https://knightsheets.com/ */
if (($_FILES['fileToUpload']['name'] != "")) {
    // Where the file is going to be stored
    $target_dir = 'files/';
    $file = $_FILES['fileToUpload']['name'];
    $path = pathinfo($file);
    $filename = $path['filename'];
    $ext = $path['extension'];
    $temp_name = $_FILES['fileToUpload']['tmp_name'];
    $path_filename_ext = $target_dir . $filename . "." . $ext;
    // Check if file already exists
    if (file_exists($path_filename_ext)) {
        //echo "";
    } else {
        move_uploaded_file($temp_name, $path_filename_ext);
        //  echo "";
    }
}
use \PhpOffice\PhpSpreadsheet\IOFactory;
use \PhpOffice\PhpSpreadsheet\Writer\Pdf;
//load the uploaded spreadsheet
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("$path_filename_ext");
//create a new pdf using mdf library
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
$date = date('d-m-y-' . substr((string)microtime(), 1, 8));
$date = str_replace(".", "", $date);
$newfilename = "export_" . $date . ".pdf";
try {
    $writer->save($newfilename);
    $content = file_get_contents($newfilename);
} catch (Exception $e) {
    exit($e->getMessage());
}
//make the file downloadable
header("Content-Disposition: attachment; filename=" . $newfilename);
unlink($newfilename);
exit($content);

8. Start the Apache server and open the Exceltopdf on the browser

Final Thoughts

Converting Excel to PDF makes it easier for people to share and distribute documents. You can view a PDF file using various devices since it does not require specific software. The immeasurable benefits are the main reason why people convert Excel to PDF. Excel Comes with an inbuilt Excel to PDF converter. However, if you do not have the software or are using a mobile phone, you can use an online converter.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.