This is an archived forum post. The information may be outdated. Contact us if you have any questions.

Creating PDFs as part of a CRON job

globalguide wrote on 2011-11-01:
I'm trying to create PDFs as part of a CRON job.

While the PDF was created successfully by running my PHP API in a browser, the CRON job failed to create the PDF running the same webpage.

It is helps, the CRON job output reported:

%PDF-1.5 1 0 obj<</Length 780/Filter/FlateDecode>>stream

but I saw no PDF creation.

Here is my code:


<?php
require 'pdfcrowd.php';

try
{
// create an API client instance
$client = new Pdfcrowd("globalguide", "xxxxxxxxxxxxxxxxxxxxxxx"); // I used my real key :)
$today = date("Ymd");

$filename = "newsletter/daily".$today.".pdf";

$handle = fopen($filename, "w");

$x = $client->setPageWidth("210mm");
$x = $client->setPageHeight("297mm");

// convert a web page and store the generated PDF into a $pdf variable
$pdf = $client->convertURI('http://www.astronautical-news.com/edition.html', $handle);

// set HTTP response headers
header("Content-Type: application/pdf");
header("Cache-Control: no-cache");
header("Accept-Ranges: none");
header("Content-Disposition: attachment; filename=\"astronautical-news.pdf\"");

// send the generated PDF
echo $pdf;
}
catch(PdfcrowdException $e)
{
echo "Pdfcrowd Error: " . $e->getMessage();
}
?>
support wrote on 2011-11-02:
It seems that the file is not flushed, try to close it with fclose(). Also, you should explicitly specify binary mode when creating the file, i.e. use "wb" for the mode parameter.
<?php 
require 'pdfcrowd.php'; 

try 
{ 
  // create an API client instance
  $client = new Pdfcrowd("globalguide", "xxxxxxxxxxxxxxxxxxxxxxx"); // I used my real key :) 
  $today = date("Ymd"); 

  $filename = "newsletter/daily".$today.".pdf"; 

  $handle = fopen($filename, "wb"); 

  $client->setPageWidth("210mm"); 
  $client->setPageHeight("297mm"); 

  // convert a web page and store the generated PDF into a $pdf variable 
  $client->convertURI('http://www.astronautical-news.com/edition.html', $handle);
  fclose($handle);
} 
catch(PdfcrowdException $e) 
{ 
echo "Pdfcrowd Error: " . $e->getMessage(); 
} 
?>


Alternatively, you could use the following command line in your crontable instead of the php script:
curl -F username=globalguide \
     -F key=xxxxxxxxxx \
     -F width=210mm \
     -F height=297mm \
     -F src=http://www.astronautical-news.com/edition.html \
     http://pdfcrowd.com/api/pdf/convert/uri/ > daily`date "+%Y%m%d"`.pdf



Does this help?
globalguide wrote on 2011-11-02:
The first way did not work and I think the second way may have worked with a correctly formed output filename but I do not know the rules for CRON job filename conventions - all I know is that daily`date "+%Y%m%d"`.pdf cannot be resolved:

/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file
support wrote on 2011-11-02:
One thing is that you should be probably using an absolute path when your code is called from a cron job. Instead of newsletter/dailyYmd.pdf it should be /absolute/path/to/newsletter/dailyYmd.pdf

You are right regarding the filename in a cron table. Please, try this one instead:
/absolute/path/to/newsletter/daily$(date +\%Y\%m\%d).pdf


Regarding PHP: The modified PHP code I posted should work. The fclose() function should ensure that the entire file is written to the disc. Did you try the code I posted? What is the size of the generated pdf file on the disc?
globalguide wrote on 2011-11-02:
No file was created by the PDF.html version. The Cron job output this:-

%PDF-1.5 1 0 obj<</Length 808/Filter/FlateDecode>>stream
globalguide wrote on 2011-11-02:
Breaking news: It WAS the filename - both CRON versions working fine now. Thanks :)