Skip to content

Commit

Permalink
Add PDF output methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni committed Sep 11, 2023
1 parent 57866da commit f601512
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 12 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* SOFTWARE : tc-lib-pdf
* AUTHOR : Nicola Asuni <[email protected]>
* COPYRIGHT : 2002-2022 Nicola Asuni - Tecnick.com LTD
* COPYRIGHT : 2002-2023 Nicola Asuni - Tecnick.com LTD
**********************************************************************

This is free software: you can redistribute it and/or modify it
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ BUT THE CORE LIBRARY STILL REQUIRES A SIGNIFICANT AMOUNT OF WORK TO BE COMPLETED
* **category** Library
* **package** \Com\Tecnick\Pdf
* **author** Nicola Asuni <[email protected]>
* **copyright** 2002-2022 Nicola Asuni - Tecnick.com LTD
* **copyright** 2002-2023 Nicola Asuni - Tecnick.com LTD
* **license** http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
* **link** https://tcpdf.org
* **source** https://github.com/tecnickcom/tc-lib-pdf
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8.0.26
8.0.27
16 changes: 8 additions & 8 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
$pdf->setSubject('tc-lib-pdf example');
$pdf->setTitle('Example');
$pdf->setKeywords('TCPDF','tc-lib-pdf','example');
$pdf->setPDFFilename('test_index.pdf');

// ----------
// Insert fonts
Expand Down Expand Up @@ -898,13 +899,12 @@
// ----------


// PDF document as string
$doc = $pdf->getOutPDFString();
// get PDF document as raw string
$rawpdf = $pdf->getOutPDFString();

// Debug document output:
//var_export($doc);
// Various output modes:

// Save the PDF document as a file
$res = file_put_contents(OUTPUT_FILE, $doc);

echo 'OK: '.OUTPUT_FILE;
//$pdf->savePDF(dirname(__DIR__).'/target', $rawpdf);
$pdf->renderPDF($rawpdf);
//$pdf->downloadPDF($rawpdf);
//echo $pdf->getMIMEAttachmentPDF($rawpdf);
2 changes: 1 addition & 1 deletion src/MetaInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class MetaInfo extends \Com\Tecnick\Pdf\Output
*
* @var string
*/
protected $version = '8.0.26';
protected $version = '8.0.27';

/**
* Time is seconds since EPOCH when the document was created.
Expand Down
121 changes: 121 additions & 0 deletions src/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@
*/
abstract class Output
{
/**
* File name of the PDF document.
*
* @var string
*/
protected $pdffilename;

/**
* Raw encoded fFile name of the PDF document.
*
* @var string
*/
protected $encpdffilename;

/**
* Array containing the ID of some named PDF objects.
*
Expand Down Expand Up @@ -2435,4 +2449,111 @@ protected function getOnOff($val)
}
return 'OFF';
}

/**
* Render the PDF in the browser or output the RAW data in the CLI.
*
* @return string $rawpdf Raw PDF data string from getOutPDFString().
*
* @throw PdfException in case of error.
*/
public function renderPDF($rawpdf = '')
{
if (php_sapi_name() == 'cli') {
echo $rawpdf;
return;
}
if (headers_sent()) {
throw new PdfException(
'The PDF file cannot be sent because some data has already been output to the browser.'
);
}
header('Content-Type: application/pdf');
header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
header('Pragma: public');
header('Expires: Sat, 01 Jan 2000 01:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Disposition: inline; filename="'.$this->encpdffilename.'"; filename*=UTF-8\'\''
.$this->encpdffilename);
if (empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
// the content length may vary if the server is using compression
header('Content-Length: '.strlen($rawpdf));
}
echo $rawpdf;
}

/**
* Trigger the browser Download dialog to download the PDF document.
*
* @return string $rawpdf Raw PDF data string from getOutPDFString().
*
* @throw PdfException in case of error.
*/
public function downloadPDF($rawpdf = '')
{
if (ob_get_contents()) {
throw new PdfException(
'The PDF file cannot be sent, some data has already been output to the browser.'
);
}
if (headers_sent()) {
throw new PdfException(
'The PDF file cannot be sent because some data has already been output to the browser.'
);
}
header('Content-Description: File Transfer');
header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
header('Pragma: public');
header('Expires: Sat, 01 Jan 2000 01:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
// force download dialog
header('Content-Type: application/pdf');
if (strpos(php_sapi_name(), 'cgi') === false) {
header('Content-Type: application/force-download', false);
header('Content-Type: application/octet-stream', false);
header('Content-Type: application/download', false);
}
// use the Content-Disposition header to supply a recommended filename
header('Content-Disposition: attachment; filename="'.$this->encpdffilename.'";'
.' filename*=UTF-8\'\''.$this->encpdffilename);
header('Content-Transfer-Encoding: binary');
if (empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
// the content length may vary if the server is using compression
header('Content-Length: '.strlen($rawpdf));
}
echo $rawpdf;
}

/**
* Save the PDF document to a local file.
*
* @return string $rawpdf Raw PDF data string from getOutPDFString().
*/
public function savePDF($path = '', $rawpdf = '')
{
$filepath = join('/', array(realpath($path), $this->pdffilename));
$fhd = $this->file->fopenLocal($filepath, 'wb');
if (!$fhd) {
throw new PdfException('Unable to create output file: '.$filepath);
}
fwrite($fhd, $rawpdf, strlen($rawpdf));
fclose($fhd);
}

/**
* Returns the PDF as base64 mime multi-part email attachment (RFC 2045).
*
* @return string $rawpdf Raw PDF data string from getOutPDFString().
*
* @return string Email attachment as raw string.
*/
public function getMIMEAttachmentPDF($rawpdf = '')
{
return 'Content-Type: application/pdf;'."\r\n"
.' name="'.$this->encpdffilename.'"'."\r\n"
.'Content-Transfer-Encoding: base64'."\r\n"
.'Content-Disposition: attachment;'."\r\n"
.' filename="'.$this->encpdffilename.'"'."\r\n\r\n"
.chunk_split(base64_encode($rawpdf), 76, "\r\n");
}
}
16 changes: 16 additions & 0 deletions src/Tcpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ public function __construct(
$this->docmodtime = $this->doctime;
$seedobj = new \Com\Tecnick\Pdf\Encrypt\Type\Seed();
$this->fileid = md5($seedobj->encrypt('TCPDF'));
$this->setPDFFilename($this->fileid.'.pdf');
$this->unit = $unit;
$this->setUnicodeMode($isunicode);
$this->subsetfont = (bool) $subsetfont;
Expand Down Expand Up @@ -237,6 +238,21 @@ protected function setUnicodeMode($isunicode)
$this->setSpaceRegexp('/[^\S\xa0]/');
}

/**
* Set the pdf document base file name.
* If the file extension is present, it must be '.pdf' or '.PDF'.
*
* @param string $name File name.
*/
public function setPDFFilename($name)
{
$bname = basename($name);
if (preg_match('/^[\w,\s-]+(\.pdf)?$/i', $bname) === 1) {
$this->pdffilename = $bname;
$this->encpdffilename = rawurlencode($bname);
}
}

/**
* Set regular expression to detect withespaces or word separators.
* The pattern delimiter must be the forward-slash character "/".
Expand Down

0 comments on commit f601512

Please sign in to comment.