pdfmiweb/php-pdfmiweb-pdf-client

PHP client for api.pdfmiweb.com

0.2.0 2013-09-10 17:24 UTC

README

Client for PHP to use pdfmiweb API service.

To use it, first you must install composer packages running: php composer.phar install in console.

Class description

$client = new PDFMiWebPDFClient("[[APPID]]", "[[APIKEY]]", "download-dir/");

The third arguments indicates directory where you want save the retrieved PDF. If no specified, PDF are saved in "/tmp" system dir. We recommend you specify this directory to have a local copy. If you specify the dir, add the trailing slash and be sure it's writable.

Methods
$client->getPDF($params);

$params: is an array of params indicating which web page want to convert in PDF, paper size, etc. These are all possible options:

$params = array(
  "webPage" => 'http://yourwebpage.com',
  "paperSize" => "A4",
  "orientation" => "portrait",
  "paperMargins" => "0cm",
  "screenType" => "print"
);
$client->createPDF($params);

$params: is an array of params indicating which web page want to convert in PDF, paper size, etc. These are all possible options:

$params = array(
  "webPage" => 'http://yourwebpage.com',
  "paperSize" => "A4",
  "orientation" => "portrait",
  "paperMargins" => "0cm",
  "screenType" => "print"
);
$client->savePDF($filename, $stream);

$filename: the name of being saved PDF (ensure this is unique or use $client->genPDFName() instead)

$client->genPDFName();

Generates a unique random name for a PDF to save it in filesystem.

$client->downloadPDF($localFileName, $downloadFileName);

Gets a PDF stored locally and sends it to client (eg: browser or phone APP) for download. $localFileName: full location of the PDF
$downloadFileName: alternative more friendly name o serve the PDF. (optional)

Usage example

require "PDFMiWebPDFClient.php";

//Create an instance of client using credentials obtained in usuarios.pdfmiweb.com
$client = new PDFMiWebPDFClient("APPID0825c08a", "0825c08a-ae3d-400d-acac-52030b6b09a0");

//Setup required params to create, get and save PDF's
$params = array(
  "webPage" => 'http://yourwebpage.com',
  "paperSize" => "A4",
  "orientation" => "portrait",
  "paperMargins" => "0cm",
  "screenType" => "print"
);

//try to get a PDF
$getPDF = $client->getPDF($params);

if ($getPDF) {
  $pdfName = $client->genPDFName();
  $client->savePDF($pdfName, $getPDF);
  $client->downloadPDF($pdfName);
}
else {
  //If PDF does not exist, create it
  $created = $client->createPDF($params);

  if ($created) {
    //Now PDF has been created at API server, we can get and save, download or both in our case
    $getPDF = $client->getPDF($params);
    $pdfName = $client->genPDFName();
    $client->savePDF($pdfName, $getPDF);
    $client->downloadPDF($pdfName);
  }
  else {
    //we handle error if API is not responding.
    throw new Exception("Can't create the PDF on te API server.", 0);
  }
}