-1

I use psliwa PHPPdf library to generate PDF files. After I load PHPPdf library I dynamically create the xml file and save it by DOMImplementation::save('path/xmlfile.xml') on the server then let the PHPPdf read this xml file to create the PDF file.

//pdf.php file
require_once __VENDORS__ . 'psliwa/php-pdf/lib/PHPPdf/Autoloader.php';

PHPPdf\Autoloader::register();
PHPPdf\Autoloader::register(__VENDORS__ . 'psliwa/php-pdf/lib/vendor/Zend/library');
PHPPdf\Autoloader::register(__VENDORS__ . 'psliwa/php-pdf/lib/vendor/ZendPdf/library');
PHPPdf\Autoloader::register(__VENDORS__ . 'psliwa/php-pdf/lib/vendor/Imagine/lib');
$engine = isset($_GET['engine']) ? $_GET['engine'] : 'pdf';
$facade = PHPPdf\Core\FacadeBuilder::create()->setEngineType($engine)
    ->setEngineOptions(array(
        'format' => 'jpg',
        'quality' => 70,
        'engine' => 'imagick',
    ))
    ->build();

$invoice = new Invoice();
$invoice->setInvoiceId($_GET['id']);

$xml = new InvoiceXmlBuilder($invoice);
$xml->generate();
$xml->save();

$name =  'xmlfile.xml';
$documentFilename =__DIRDOCS__ .$name;
$stylesheetFilename = __DIRDOCS__ . '/xmlfile-style.xml';
... Rest of the PHPPdf code

When I do this on my local machine there is no problem however on the server I get header already sent error which is below.

Warning: Cannot modify header information - headers already sent by (output started at /path/controllers/InvoiceXmlBuilder.php:165) in /path/pdf.php on line 56

// Line 165 in InvoiceXmlBuilder.php 
echo $this->dom->save(__DIRDOCS__ . "xmlfile.xml");

// Line 56 in pdf.php
header('Content-Type: application/pdf');

Any idea why this is happening?

u54r
  • 1,767
  • 2
  • 15
  • 26

1 Answers1

0

The issue is you are echoing the result of the save operation. If that functions returns nothing, it will still echo something (producing output). This is obviously happening prior to you sending the header information, so try removing the echo statement and just $this->dom->save(...) on line 165.

Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96