Php excel to pdf converter

Well my 2 cents when it comes to the topic word 2007 docx, word 97-2004 doc, pdf and all other types of MS Office wishing to be «converted from y to z but in real they don’t wanna be». In my experience so far, conversion with LibreOffice or OpenOffice can’t be relied on. Though .doc documents tend to be better supported than word 2007’s .docx. In general it’s very hard to convert the .docx to .doc without breaking anything.

.docx also tend to be extremely useful for templating where .doc is not for being binary.

The conversion from .doc to PDF was most of the time quite reliable. If you can still influence the design or content of the word document then this might be satisfying, but in my situation documents were supplied from foreign companies where even after generating the .docx templates, in some scenario’s, the generated .docx had to be slightly modified with supplement text before it was generated to a PDF.


WINDOWS BASED!

All this hiccup made me come to the conclusion that the only true reliable conversion method I found was using the COM class in PHP and let the MS Word or Excel Application do all the work for you. I’ll just give an example on converting .docx to .doc and/or PDF. If you do not have MS Office installed, you can download a trial version of 60 days which would give you enough room for testing purposes.

the COM.net extension is by default commented out in the php.ini, just search for the line php_com_dotnet.dll and uncomment it like so

  extension=php_com_dotnet.dll

Restart the web server (IIS is not a pre, Apache will work just as well).

The code below is a demonstration on how easy it is.

  $word = new COM("Word.Application") or die ("Could not initialise Object.");
  // set it to 1 to see the MS Word window (the actual opening of the document)
  $word->Visible = 0;
  // recommend to set to 0, disables alerts like "Do you want MS Word to be the default .. etc"
  $word->DisplayAlerts = 0;
  // open the word 2007-2013 document 
  $word->Documents->Open('yourdocument.docx');
  // save it as word 2003
  $word->ActiveDocument->SaveAs('newdocument.doc');
  // convert word 2007-2013 to PDF
  $word->ActiveDocument->ExportAsFixedFormat('yourdocument.pdf', 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
  // quit the Word process
  $word->Quit(false);
  // clean up
  unset($word);

This is just a small demonstration. I can just say that if it comes to conversion, this was the only real reliable option I could use and even recommend.

In this post, you’ll learn how to convert Excel files to PDFs using PSPDFKit’s XLSX to PDF PHP API. With our API, you can convert up to 100 PDF files per month for free. All you need to do is create a free account to get access to your API key.

PSPDFKit API

Document conversion is just one of our 30+ PDF API tools. You can combine our conversion tool with other tools to create complex document processing workflows. You’ll be able to convert various file formats into PDFs and then:

  • Merge several resulting PDFs into one

  • OCR, watermark, or flatten PDFs

  • Remove or duplicate specific PDF pages

Once you create your account, you’ll be able to access all our PDF API tools.

Step 1 — Creating a Free Account on PSPDFKit

Go to our website, where you’ll see the page below, prompting you to create your free account.

Free account PSPDFKit API

Once you’ve created your account, you’ll be welcomed by the page below, which shows an overview of your plan details.

Free plan PSPDFKit API

As you can see in the bottom-left corner, you’ll start with 100 documents to process, and you’ll be able to access all our PDF API tools.

Step 2 — Obtaining the API Key

After you’ve verified your email, you can get your API key from the dashboard. In the menu on the left, click API Keys. You’ll see the following page, which is an overview of your keys:

Convert Excel to PDF PHP API Key

Copy the Live API Key, because you’ll need this for the Excel to PDF API.

Step 3 — Setting Up Folders and Files

Now, create a folder called excel_to_pdf and open it in a code editor. For this tutorial, you’ll use VS Code as your primary code editor. Next, create two folders inside excel_to_pdf and name them input_documents and processed_documents.

Now, copy your Excel file to the input_documents folder and rename it to document.xlsx. You can use our demo document as an example.

Then, in the root folder, excel_to_pdf, create a file called processor.php. This is the file where you’ll keep your code.

Your folder structure will look like this:

excel_to_pdf
├── input_documents
|    └── document.xlsx
├── processed_documents
└── processor.php

Step 4 — Writing the Code

Open the processor.php file and paste the code below into it:

<?php

$FileHandle = fopen('processed_documents/result.pdf', 'w+');

$curl = curl_init();

$instructions = '{
  "parts": [
    {
      "file": "document"
    }
  ]
}';

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.pspdfkit.com/build',
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_POSTFIELDS => array(
    'instructions' => $instructions,
    'document' => new CURLFILE('input_documents/document.xlsx')
  ),
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer YOUR API KEY HERE '
  ),
  CURLOPT_FILE => $FileHandle,
));

$response = curl_exec($curl);

curl_close($curl);

fclose($FileHandle);

ℹ️ Note: Make sure to replace YOUR_API_KEY_HERE with your API key.

Code Explanation

In the code above, you first create a FileHandle variable that will allow you to save the file in the processed_documents folder.

Then, you create the instructions variable, where all the instructions for the API will be stored in the form of a JSON string. Finally, you make a CURL request.

Output

To execute the code, run the command below:

On successful execution, you’ll see a new processed file, result.pdf, located in the processed_documents folder.

The folder structure will look like this:

excel_to_pdf
├── input_documents
|    └── document.xlsx
├── processed_documents
|    └── result.pdf
└── processor.php

Final Words

In this post, you learned how to easily and seamlessly convert Excel files to PDF documents for your PHP application using our Excel to PDF PHP API.

You can integrate all of these functions into your existing applications. With the same API token, you can also perform other operations, such as merging several documents into a single PDF, adding watermarks, and more. To get started with a free trial, sign up here.

18.01.2019

Задача сохранении XLSX в PDF на php

Библиотека phpoffice/phpspreadsheet (подробнее в статье Работа с XLSX), среди всего прочего позволяет сохранять документы в нужном формате.

Для начала установил phpspreadsheet:

 
composer require phpoffice/phpspreadsheet

Но при первом же тесте, взятом из документации, получил ошибку

Fatal error: Uncaught PhpOfficePhpSpreadsheetWriterException: No writer found for type Pdf,
дело в том, что работу с PDF phpoffice/phpspreadsheet производит через сторонние библиотеки и их нужно ставить отдельно.

Для первого теста выбрал mPDF

Установил ее:

 
composer require mpdf/mpdf

И сразу получил сообщение open_basedir restriction in effect. File(/tmp) is not within the allowed path, а также выяснилось, что съедалось любое количество выделенной оперативной памяти.
Решилось проблема редактированием файла настроек хоста: php_admin_value open_basedir «/home/public/NAME.ru/:/tmp»

Результат был такой:

pic1

Сам код:

 
require_once('vendor/autoload.php');

use PhpOfficePhpSpreadsheetIOFactory;
use PhpOfficePhpSpreadsheetSpreadsheet;
use PhpOfficePhpSpreadsheetReaderXlsx;
use PhpOfficePhpSpreadsheetWriterPdf;

$oReader = new Xlsx();
$oSpreadsheet = $oReader->load('i_Акт.xlsx');
$oWriter = IOFactory::createWriter($oSpreadsheet, 'Mpdf');

$oWriter->save('i_Акт.pdf');

Вторым выбрал Dompdf

Установка:

 
 composer require dompdf/dompdf

Изменение кода:

 
$oWriter = IOFactory::createWriter($oSpreadsheet, 'Dompdf'); // Mpdf => Dompdf

Результат получился лучше, но все равно не тот:

pic1

При тестоах выяснилось, что проблема именно в Dompdf:

 
use DompdfDompdf;

$dompdf = new Dompdf();
//$dompdf->set_option('defaultFont', 'dejavu sans');
//$dompdf->loadHtml( iconv("UTF-8", "windows-1251", 'Супер тест!' . rand(-10, 10) ) );

$dompdf->loadHtml( '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <h1>Супер</h1> тест!'. rand(-10, 10), 'UTF-8' );

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();

— тоже выдает не верную кодировку.

Попробовал заменить его стандартные шрифты на такие же, но с поддержкой кириллиц — результат не получил.

Временно отложил эту библиотеку.

Третий тест был для TCPDF

Установка:

 
composer require tecnickcom/tcpdf

Изменение кода:

 
$oWriter = IOFactory::createWriter($oSpreadsheet, 'Tcpdf'); // Tcpdf

Первый Вариант, оказался не очень:

pic1

В заключении

Лучший вариант был у библиотеки mPDF

Для остальных нужно тестировать разные настройки, как это сделать описывается в документации в разделе Custom implementation or configuration

 

class My_Custom_TCPDF extends TCPDF
{
    // ...
}

class My_Custom_TCPDF_Writer extends PhpOfficePhpSpreadsheetWriterPdfTcpdf
{
    protected function createExternalWriterInstance($orientation, $unit, $paperSize)
    {
        $instance = new My_Custom_TCPDF($orientation, $unit, $paperSize);

        // more configuration of $instance

        return $instance;
    }
}

PhpOfficePhpSpreadsheetIOFactory::registerWriter('Pdf', MY_TCPDF_WRITER::class);


Категории: PHP

To convert your first file with the Zamzar API, send an HTTP request to POST https://sandbox.zamzar.com/v1/jobs containing your source file, and the your desired target format. If the source file is on the web or in S3, send us the URL: the source file doesn’t need to hit your servers.

<?php

$endpoint = "https://sandbox.zamzar.com/v1/jobs";
$apiKey = "GiVUYsF4A8ssq93FR48H";
$sourceFile = "https://s3.amazonaws.com/zamzar-samples/sample.xlsx";
$targetFormat = "PDF";

$postData = array(
  "source_file" => $sourceFile,
  "target_format" => $targetFormat
);

$ch = curl_init(); // Init curl
curl_setopt($ch, CURLOPT_URL, $endpoint); // API endpoint
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as a string
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":"); // Set the API key as the basic auth username
$body = curl_exec($ch);
curl_close($ch);

$response = json_decode($body, true);

echo "Response:n---------n";
print_r($response);

Your source file is now being converted. Send an HTTP request to GET https://sandbox.zamzar.com/v1/jobs/$jobId to check its progress. The response will also give you details about your converted file.

<?php

$jobID = 15;
$endpoint = "https://sandbox.zamzar.com/v1/jobs/$jobID";
$apiKey = "GiVUYsF4A8ssq93FR48H";

$ch = curl_init(); // Init curl
curl_setopt($ch, CURLOPT_URL, $endpoint); // API endpoint
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as a string
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":"); // Set the API key as the basic auth username
$body = curl_exec($ch);
curl_close($ch);

$job = json_decode($body, true);

echo "Job:n----n";
print_r($job);

Once the status of your job is successful, your converted file is ready to download. Send an HTTP request to GET https://sandbox.zamzar.com/v1/file/$fileId/content to download it. We store your files for a day by default, and for longer on our paid plans.

<?php

$fileID = 3;
$localFilename = "converted.pdf";;
$endpoint = "https://sandbox.zamzar.com/v1/files/$fileID/content";
$apiKey = "GiVUYsF4A8ssq93FR48H";

$ch = curl_init(); // Init curl
curl_setopt($ch, CURLOPT_URL, $endpoint); // API endpoint
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":"); // Set the API key as the basic auth username
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

$fh = fopen($localFilename, "wb");
curl_setopt($ch, CURLOPT_FILE, $fh);

$body = curl_exec($ch);
curl_close($ch);

echo "File downloadedn";

If you like what you see and want to start converting files under your own API account then please click the «Get Started Now» button to signup for your own API account. Please feel free to get in touch with us should you have any specific questions or refer to our extensive docs and FAQ for further information.

Like this post? Please share to your friends:
  • Php excel spreadsheet excel writer
  • Php excel spreadsheet excel reader
  • Php excel read excel file
  • Php excel parser что это
  • Php excel from html