3

I was googling many hours but I haven't got solution yet.

I need to convert .xls file into .pdf file. How to do that? I have found this http://www.aspose.com/, but it seems, that it hasn't got PHP API, only .NET and JAVA.

Thanks for every advice...

David Holada
  • 109
  • 1
  • 2
  • 14
  • 2
    Maybe use PHP EXCEL https://phpexcel.codeplex.com/ to read it and TCPDF http://www.tcpdf.org/ to output it – Pwner Apr 22 '14 at 15:16
  • 2
    possible duplicate of [Convert Word doc, docx and Excel xls, xlsx to PDF with PHP](http://stackoverflow.com/questions/5538584/convert-word-doc-docx-and-excel-xls-xlsx-to-pdf-with-php) – Dinistro Apr 22 '14 at 15:17
  • Yes, I'm using PHP EXCEL to edit .xls, but TCPDF convertor always totally broke the .xls structure... In TCPDF documentation I even didn't find any function to convert .xls to .pdf... – David Holada Apr 22 '14 at 15:21
  • 1
    PHPExcel provides a PDF Writer, which can be used as a wrapper for tcPDF, mPDF or DomPDF as an actual PDF rendering engine... these three different engines provide different quality of output, but IMO tcPDF is the worst – Mark Baker Apr 22 '14 at 15:22

3 Answers3

6

PHPExcel will do it directly (read XLS, output PDF), although the results are not very pretty.

A better solution might be to use PHPExcel to read the XLS file, render it to HTML in your code & style it the way you like it, then use an HTML->PDF converter like DOMPDF (that's the only one I've tried; there are others) to convert to PDF.

Or, if you want to skip the HTML step, render it directly to PDF using one of the PHP PDF libraries out there - my personal favourite is FPDF.

Kryten
  • 15,230
  • 6
  • 45
  • 68
  • 1
    Yes thanks, but how can I connect PHPEXCEL with FPDF? How can I edit this lines $rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF; $rendererLibrary = 'fpdf'; $rendererLibraryPath = 'fpdf/' . $rendererLibrary;? – David Holada Apr 22 '14 at 15:48
  • 1
    Sorry, I didn't mean use FDPF as the renderer for PHPExcel; I meant load your XLS file, walk through the cells (in whatever way you want), and use FPDF to build the PDF document from scratch. A lot of work, I know, but FPDF makes it very easy to build a PDF document. – Kryten Apr 22 '14 at 16:03
2

use headless libreoffice and a shell command shell_exec('C:\wamp64\www\phpexcel\lo\App\libreoffice\program\soffice.exe --headless --convert-to pdf:calc_pdf_Export --outdir C:\wamp64\www\phpexcel\o C:\wamp64\www\phpexcel\new.xlsx');

JuKe
  • 663
  • 2
  • 7
  • 20
  • This works like a charm in Ubuntu -- provided you need to correct syntax for linux. Adding the exact command in below as answer to this for someone who is looking the same. – Sandipan S Aug 21 '19 at 07:44
1

Anyone who is looking to do this in Ubuntu/linux using php -

Ubuntu comes with libre office installed default. Anyone can use the shell command to use the headless libre office for this.

shell_exec('/usr/bin/libreoffice --headless --convert-to pdf:calc_pdf_Export --outdir /var/www/html/XlsToPdf/public_html/src/var/output /var/www/html/XlsToPdf/public_html/src/var/source/company-a.xlsx');

Hope it helps a others like me.

Sandipan S
  • 121
  • 4