web development

How To Convert PDF To Excel Without Losing Formatting

  • imageSomish Kakadiya
  • iconJun 24, 2022
  • icon130
  • icon
  • icon
image

We need to convert our different types of files or documents into Excel for calculations and data manipulation reasons.

And PDF is one of them, a widely used document format and an important source of data for businesses. Therefore, we have created an example for you to understand this process of conversion.

Step 1

Create the blade.php file page to download your PDF file using Bootstrap.







Step 2

Write route in web.php:

Route::post('convertToPDF', 'PDFConvertorController@convertToPDF');

Step 3

Installation PHP Word

Run the following to use the latest stable version:

composer require phpoffice/phpword

Or if you want the latest master version:

composer require phpoffice/phpword:dev-master

You can of course also manually edit your composer.json file.

Step 4

Installation PHP Spreadsheet

Run the following to use the latest stable version:

composer require phpoffice/phpspreadsheet

You can of course also manually edit your composer.json file.

Step 5

Getting Started

Make PDFConvertorController.php in Controller.

Write following function in this controller :

Conclusion:

Wrapping up the blog with a step-by-step guide on converting PDF to Excel without losing the format. 

It will be very helpful in maintaining your business documents. We have presented ready-to-use codes for you. That will make you understand the process in a better way.

Documenttitle><br>14.  <br>15.  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"><br>16.  <br>17.  head><br>18.  <br>19.  <body><br>20.  <br>21.  <div class="container"><br>22.  <br>23.  <h2>Convert To PDFh2><br>24.  <br>25.  <form action="{{url('convertToPDF')}}" method="post" enctype="multipart/form-data"><br>26.  <br>27.  {{ csrf_field() }}<br>28.  <br>29.  <div class="form-group"><br>30.  <br>31.  <label for="file">File:label><br>32 <br>33.  <input type="file" name="file" id="file"><br>34.  <br>35.  div><br>36.  <br>37.  <input type="submit" value="Download" class="btn btn-primary"><br>38  <br>39.  form><br>40.  <br>41.  div><br>42.  <br>43.  body><br>44.  <br>45.  html></p> <p><a href="https://gist.github.com/DiganVaghasiya/3ef267396d8e89b9a062ffe7f1a95229/raw/773cd79c55304c2f6533aace659f6b3fe0a58412/gistfile1.html"><strong>View Raw</strong></a></p> <p> </p> <h4><img style="display: block; margin-left: auto; margin-right: auto;" src="https://outsourcingvasundhara.sfo3.cdn.digitaloceanspaces.com/public/blogs/pdf-to-excel-1.webp" alt="" width="980" height="506"></h4> <h4> </h4> <h4>Step 2</h4> <p>Write route in web.php:</p> <p>Route::post('convertToPDF', 'PDFConvertorController@convertToPDF');</p> <p> </p> <p><img src="https://outsourcingvasundhara.sfo3.cdn.digitaloceanspaces.com/public/blogs/pdf-to-excel-2.webp" alt="" width="592" height="38"></p> <p> </p> <h4>Step 3</h4> <p><strong>Installation PHP Word</strong></p> <p>Run the following to use the latest stable version:</p> <p><strong>composer require phpoffice/phpword</strong></p> <p>Or if you want the latest master version:</p> <p><strong>composer require phpoffice/phpword:dev-master</strong></p> <p>You can of course also manually edit your composer.json file.</p> <ol> <li>{</li> <li> </li> <li><span class="pl-ent">"require"</span>: {</li> <li> </li> <li><span class="pl-ent">"phpoffice/phpword"</span>: <span class="pl-s"><span class="pl-pds">"</span>v0.16.*<span class="pl-pds">"</span></span></li> <li> </li> <li><span class="pl-s">}</span></li> <li> </li> <li><span class="pl-s">}</span></li> </ol> <p><a href="https://gist.githubusercontent.com/DiganVaghasiya/ef6c8a7cb7d930f6d764d1639388c3c6/raw/96ccdabc1858b201992f18f28d0baa11d9c1c817/gistfile1.json"><strong><span class="pl-s">View Raw</span></strong></a></p> <h4>Step 4</h4> <p>Installation PHP Spreadsheet</p> <p>Run the following to use the latest stable version:</p> <p>composer require phpoffice/phpspreadsheet</p> <p>You can of course also manually edit your composer.json file.</p> <ol> <li>{</li> <li> </li> <li>"require": {</li> <li> </li> <li>"phpoffice/phpspreadsheet": "^1.4",</li> <li> </li> <li>}</li> <li> </li> <li>}</li> </ol> <p><a href="https://gist.github.com/DiganVaghasiya/0f2a9b5b98ec7ca0ebf17e217f9dc03d/raw/0e9dc6084550c11a8462b22d60fa6e99643ace73/gistfile1.txt"><strong>View Raw</strong></a></p> <h4>Step 5</h4> <p><strong>Getting Started</strong></p> <p>Make PDFConvertorController.php in Controller.</p> <p>Write following function in this controller:</p> <ol> <li>public function convertToPDF(Request $request){</li> <li> </li> <li> </li> <li> </li> <li>$input = $request->all();</li> <li> </li> <li> </li> <li> </li> <li>if ($request->hasFile('file')) {</li> <li> </li> <li>$file = $request->file('file');</li> <li> </li> <li>$name = time().'.'.$file->getClientOriginalExtension();</li> <li> </li> <li>$destinationPath = public_path('/file');</li> <li> </li> <li>$file->move($destinationPath, $name);</li> <li> </li> <li>$input['file'] = $name;</li> <li> </li> <li>}</li> <li> </li> <li> </li> <li> </li> <li>$pdfname = time().'.pdf';</li> <li> </li> <li>$FilePath = public_path()."/file/".$name;</li> <li> </li> <li>$FilePathPdf = public_path()."/pdf_file/".$pdfname;</li> <li> </li> <li> </li> <li> </li> <li>$extension = $file->getClientOriginalExtension();</li> <li> </li> <li>/* for excel sheet to pdf */</li> <li> </li> <li>if($extension == 'xls' || $extension == 'xlsx'){</li> <li> </li> <li> </li> <li> </li> <li>$phpWord_xls = \PhpOffice\PhpSpreadsheet\IOFactory::load($FilePath);</li> <li> </li> <li>$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($phpWord_xls, 'Dompdf');</li> <li> </li> <li>$objWriter->save($FilePathPdf);</li> <li> </li> <li>$file = public_path()."/pdf_file/".$pdfname;</li> <li> </li> <li>}</li> <li> </li> <li> </li> <li> </li> <li>/* for doc or docx to pdf */</li> <li> </li> <li>elseif ($extension == 'doc') {</li> <li> </li> <li> </li> <li> </li> <li>\PhpOffice\PhpWord\Settings::setPdfRendererPath(base_path() .'/vendor/dompdf/dompdf');</li> <li> </li> <li>\PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');</li> <li> </li> <li> </li> <li> </li> <li>$phpWord = \PhpOffice\PhpWord\IOFactory::load($FilePath, 'MsDoc');</li> <li> </li> <li>$pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter( $phpWord, 'PDF' );</li> <li> </li> <li>$pdfWriter->save($FilePathPdf);</li> <li> </li> <li>$file = public_path()."/pdf_file/".$pdfname;</li> <li> </li> <li> </li> <li> </li> <li>}</li> <li> </li> <li>else{</li> <li> </li> <li>\PhpOffice\PhpWord\Settings::setPdfRendererPath(base_path() .'/vendor/dompdf/dompdf');</li> <li> </li> <li>\PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');</li> <li> </li> <li> </li> <li> </li> <li>$phpWord = \PhpOffice\PhpWord\IOFactory::load($FilePath);</li> <li> </li> <li>$pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter( $phpWord, 'PDF' );</li> <li> </li> <li>$pdfWriter->save($FilePathPdf);</li> <li> </li> <li>$file = public_path()."/pdf_file/".$pdfname;</li> <li> </li> <li> </li> <li> </li> <li>}</li> <li> </li> <li>$headers = array(</li> <li>.</li> <li>'Content-Type: application/pdf',</li> <li> </li> <li>);</li> <li> </li> <li>return response()->download($file, 'filename.pdf', $headers);</li> <li> </li> <li> </li> <li> </li> <li>}</li> </ol> <p><strong><a href="https://gist.github.com/DiganVaghasiya/c9eb625fd5640ac8a952467e97f196af/raw/d2e9ec2c169995ff8c3b2f375e5945cd39037823/gistfile1.txt">View Raw</a></strong></p> <h4>Conclusion</h4> <p>Wrapping up the blog with a step-by-step guide on converting PDF to Excel without losing the format.</p> <p>It will be very helpful in maintaining your business documents. We have presented ready-to-use codes for you. That will make you understand the process in a better way.</p>

Your Future,

Our Focus

  • user
  • user
  • user
  • user

Start Your Digital Transformation Journey Now and Revolutionize Your Business.

0+
Years of Shaping Success
0+
Projects Successfully Delivered
0x
Growth Rate, Consistently Achieved
0+
Top-tier Professionals