Problems Viewing PDF Inline Or Attaching

Topics: Developer Forum
Jan 11, 2010 at 12:05 PM

I have PHP v5.3.0 installed on my Linux server.  The settings are:

Configure Command

'./configure' '--build=i686-pc-linux-gnu' '--host=i686-pc-linux-gnu' '--target=i386-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/usr/com' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--cache-file=../config.cache' '--with-libdir=lib' '--with-config-file-path=/etc' '--with-config-file-scan-dir=/etc/php.d' '--disable-debug' '--with-pic' '--disable-rpath' '--without-pear' '--with-bz2' '--with-curl' '--with-exec-dir=/usr/bin' '--with-freetype-dir=/usr' '--with-png-dir=/usr' '--enable-gd-native-ttf' '--without-gdbm' '--with-gettext' '--with-gmp' '--with-iconv' '--with-jpeg-dir=/usr' '--with-openssl' '--with-png' '--with-pspell' '--with-expat-dir=/usr' '--with-pcre-regex=/usr' '--with-zlib' '--with-layout=GNU' '--enable-exif' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-sysvmsg' '--enable-track-vars' '--enable-trans-sid' '--enable-yp' '--enable-wddx' '--with-kerberos' '--with-unixODBC=shared,/usr' '--enable-memory-limit' '--enable-shmop' '--enable-calendar' '--enable-dbx' '--enable-dio' '--with-mime-magic=/usr/share/file/magic.mime' '--without-sqlite' '--with-libxml-dir=/usr' '--with-xml' '--enable-force-cgi-redirect' '--enable-pcntl' '--with-imap=shared' '--with-imap-ssl' '--enable-mbstring=shared' '--enable-mbstr-enc-trans' '--enable-mbregex' '--with-ncurses=shared' '--with-gd' '--enable-bcmath=shared' '--enable-dba=shared' '--with-db4=/usr' '--with-xmlrpc=shared' '--with-ldap=shared' '--with-mysql=shared,/usr' '--with-mysqli=shared,/usr/bin/mysql_config' '--enable-dom' '--with-dom-xslt=/usr' '--with-dom-exslt=/usr' '--with-pgsql=shared' '--enable-soap=shared' '--with-xsl=shared,/usr' '--enable-xmlreader' '--enable-xmlwriter' '--enable-fastcgi' '--enable-pdo=shared' '--with-pdo-odbc=shared,unixODBC,/usr' '--with-pdo-mysql=shared,/usr' '--with-pdo-pgsql=shared,/usr' '--with-pdo-sqlite=shared,/usr' '--with-readline' '--with-tidy' '--with-apxs2=/usr/sbin/apxs' '--enable-zip'

Additional .ini files parsed

/etc/php.d/bcmath.ini, /etc/php.d/dba.ini, /etc/php.d/dom.ini, /etc/php.d/gd.ini, /etc/php.d/imap.ini, /etc/php.d/ldap.ini, /etc/php.d/mbstring.ini, /etc/php.d/mysqli.ini, /etc/php.d/mysql.ini, /etc/php.d/ncurses.ini, /etc/php.d/odbc.ini, /etc/php.d/pdo.ini, /etc/php.d/pdo_mysql.ini, /etc/php.d/pdo_odbc.ini, /etc/php.d/pdo_pgsql.ini, /etc/php.d/pdo_sqlite.ini, /etc/php.d/pgsql.ini, /etc/php.d/soap.ini, /etc/php.d/xmlreader.ini, /etc/php.d/xmlrpc.ini, /etc/php.d/xmlwriter.ini, /etc/php.d/xsl.ini

Registered PHP Streams

https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip

Registered Stream Socket Transports

tcp, udp, unix, udg, ssl, sslv3, sslv2, tls

Registered Stream Filters

zlib.*, bzip2.*, convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk


I am running this script using PHPExcel v1.6.7:

<?
//set timezone
date_default_timezone_set('America/New_York');

/** Error reporting */
error_reporting(E_ALL);

/** Include path **/
ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.'../private/utilities/PHPExcel/Classes/');

/** PHPExcel */
include 'PHPExcel.php';

/** PHPExcel_Writer_PDF */
include 'PHPExcel/Writer/PDF.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set properties
$objPHPExcel->getProperties()->setCreator('Maarten Balliauw');
$objPHPExcel->getProperties()->setLastModifiedBy('Maarten Balliauw');
$objPHPExcel->getProperties()->setTitle('Office 2007 XLSX Test Document');
$objPHPExcel->getProperties()->setSubject('Office 2007 XLSX Test Document');
$objPHPExcel->getProperties()->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.');

// redirect output to client browser
header('Content-Type: application/pdf');
header('Connection: Close');
header('Content-Disposition: attachment;filename="test.pdf"');
//header('Content-Disposition: inline');
//header('Content-Length: 499096');
//header('Accept-Ranges: bytes');

//output a PDF file
$objWriter = new PHPExcel_Writer_PDF($objPHPExcel);
$objWriter->setTempDir('/home/virtual/site1/fst/var/www/html');
$objWriter->save('test.pdf');
?>


I am having three problems:

1) I can't save the temp PDF file in my /tmp folder even though the tmp folder's permissions are set to 0x777 and I replaced the $objWriter->setTempDir('/home/virtual/site1/fst/var/www/html'); line above with $objWriter->setTempDir('/tmp');  For some reason, the file is saved in the /home/virtual/site1/fst/var/www/html directory.

2) I can't view the PDF file in the browser.  If I comment out the header('Content-Disposition: attachment;filename="test.pdf"'); line above and uncomment the next three lines, I get a blank page in my browser.  I tried this on FF v3.0.15 and IE 7.

3) I get a corrupt file when downloading the PDF file as an attachment.  I had to comment out lines 5540 and 5583 in PHPExcel/Classes/PHPExcel/Shared/PDF/tcpdf.php file because of a fatal error based on set_magic_quotes_runtime() being deprecated as of PHP v5.3.0.  I also had to replace 'split' with 'explode' because of deprecation in line 10775 of the same file.  When I try to open the file in Acrobat I get this error:

Acrobat could not open 'test.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).

Could this be connected to the commenting out of lines 5540 and 5583?  I viewed test.pdf in a hex editor.  The file is blank.

 

Thank you.

Developer
Jan 14, 2010 at 3:35 AM

Thanks for your report. For a start, can you try to download latest source from here tomorrow:

http://phpexcel.codeplex.com/SourceControl/list/changesets

What happens when you run this test:

Tests/01simple-download-pdf.php

 

Jan 15, 2010 at 10:14 PM

I downloaded the latest version of PHPExcel and brought it up to date via SVN but I'm still getting the same error when I run Tests/01simple-download-pdf.php (downloading as an attachment).  I tried viewing the PDF inline and got this error from Acrobat:

File does not begin with '%PDF-'.

Developer
Jan 17, 2010 at 3:51 AM
lldc wrote:

I downloaded the latest version of PHPExcel and brought it up to date via SVN but I'm still getting the same error when I run Tests/01simple-download-pdf.php (downloading as an attachment).  I tried viewing the PDF inline and got this error from Acrobat:

File does not begin with '%PDF-'.

This is strange. Can you take that PDF file and open it in a text editor so check that it does not begin with '%PDF-' ? What do you see?

Jan 17, 2010 at 4:48 AM

As soon as I sent my last resply I remembered that I didn't look at the faulty PDF in a hex editor.  I just did that and relized that the PDF has these warnings:

Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EST/-5.0/no DST' instead in /home/virtual/site1/fst/var/www/private/utilities/PHPExcel/Classes/PHPExcel/Shared/PDF/tcpdf.php on line 7316

Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EST/-5.0/no DST' instead in /home/virtual/site1/fst/var/www/private/utilities/PHPExcel/Classes/PHPExcel/Shared/PDF/tcpdf.php on line 7316

I added date_default_timezone_set('America/New_York'); to the top of Tests/01simple-download-pdf.php and everything works fine now - download as attachment and view inline.  Thanks for your help.