Re: How Joomla 3.8 generates PDF now? Lack of pdf.php, new libraries structure

Started by Adwans, March 23, 2018, 16:24:49 PM

Previous topic - Next topic

Adwans

Of course, here you are. My Unicode dejavusans font is added, I commented some points useless for me. You can make it refined or better coded. Regards :)

<?php

namespace Joomla\CMS\Document;

defined('JPATH_PLATFORM') or die;

use 
Joomla\Filesystem\File// phoca
use Joomla\CMS\Factory;



class 
PdfDocument extends Document
{

public $_charset  'utf-8';
public $_mime  'application/pdf';
public $_type  'pdf';

 //
public $_engine null;

protected $_name 'joomla';

public $_header null;
public $_header_font 'dejavusans';
public $_footer_font 'dejavusans';

public $_margin_header 5;
public $_margin_footer 10;
public $_margin_top 27;
public $_margin_bottom 25;
public $_margin_left 15;
public $_margin_right 15;

// Scale ratio for images [number of points in user unit]
public $_image_scale 4;

/**
 * Class constructore
 *
 * @access protected
 * @param array $options Associative array of options
 */
function __construct($options = array())
{
parent::__construct($options);


if(!file_exists(JPATH_LIBRARIES.'/tcpdf/tcpdf.php')){
\JError::raiseError(500\JText::sprintf('JLIB_APPLICATION_ERROR_APPLICATION_LOAD','TCPDF'));
} else {
if(!class_exists('TCPDF')) \JLoader::register('TCPDF'JPATH_LIBRARIES '/tcpdf/tcpdf.php');
}

/**** 7 4 wyłączam

if (isset($options['margin-header'])) { $this->_margin_header = $options['margin-header']; }

if (isset($options['margin-footer'])) { $this->_margin_footer = $options['margin-footer']; }

if (isset($options['margin-top'])) { $this->_margin_top = $options['margin-top']; }

if (isset($options['margin-bottom'])) { $this->_margin_bottom = $options['margin-bottom']; }

if (isset($options['margin-left'])) { $this->_margin_left = $options['margin-left']; }

if (isset($options['margin-right'])) { $this->_margin_right = $options['margin-right']; }

if (isset($options['image-scale'])) { $this->_image_scale = $options['image-scale']; }
7 4 ****/


$this->_mime 'application/pdf';
$this->_type 'pdf';


/*
 * Create the pdf document
 */
// Default settings are a portrait layout with an A4 configuration using millimeters as units


if(class_exists('TCPDF'))
        {

    
}else {
throw new Exception('Juz zaladowany tcpdf'500);
return false;
}
 
$this->_engine = new \TCPDF();

//set margins
$this->_engine->SetMargins($this->_margin_left$this->_margin_top$this->_margin_right);
//set auto page breaks
$this->_engine->SetAutoPageBreak(TRUE$this->_margin_bottom);
$this->_engine->SetHeaderMargin($this->_margin_header);
$this->_engine->SetFooterMargin($this->_margin_footer);
$this->_engine->setImageScale($this->_image_scale);



}

 /**
 * Sets the document name
 *
 * @param   string   $name Document name
 * @access  public
 * @return  void
 */
function setName($name 'joomla') {
$this->_name $name;
}

/**
 * Returns the document name
 *
 * @access public
 * @return string
 */
function getName() {
return $this->_name;
}

 /**
 * Sets the document header string
 *
 * @param   string   $text Document header string
 * @access  public
 * @return  void
 */
function setHeader($text) {
$this->_header $text;
}

/**
 * Returns the document header string
 *
 * @access public
 * @return string
 */
function getHeader() {
return $this->_header;
}

/**
 * Render the document.
 *
 * @access public
 * @param boolean  $cache If true, cache the output
 * @param array $params Associative array of attributes
 * @return  The rendered data
 */
   
function render$cache false$params = array())
{

if(class_exists('TCPDF'))
        {   
}else {
throw new Exception('Brak zaladowanego tcpdf'500);
return false;
}
#end


$pdf = &$this->_engine


// Set PDF Metadata
$pdf->SetCreator($this->getGenerator());
$pdf->SetTitle($this->getTitle());
$pdf->SetSubject($this->getDescription());
//$pdf->SetKeywords($this->getMetaData('keywords')); 7 4

// Set PDF Header data
$pdf->setHeaderData('',0,$this->getTitle(), $this->getHeader());

// Set PDF Header and Footer fonts
$lang \JFactory::getLanguage();

$pdf->setRTL($lang->isRTL());
                
//$pdf->SetFont('helvetica', '', 8, '', 'false');
$pdf->SetFont('dejavusans'''8'''false');

$pdf->setHeaderFont(array($this->_header_font''10));
$pdf->setFooterFont(array($this->_footer_font''8));



// Initialize PDF Document
//debug 7 4 
$pdf->getAliasNbPages();
$pdf->AddPage();

// Build the PDF Document string from the document buffer
// niepotrzebne:
//$this->fixLinks();
$pdf->WriteHTML($this->getBuffer(), true);
$data $pdf->Output('''S');
// Set document type headers
parent::render();

//JResponse::setHeader('Content-Length', strlen($data), true); komentarz był
//\JResponse::setHeader('Content-type', 'application/pdf', true);
//\JResponse::setHeader('Content-disposition', 'inline; filename="'.$this->getName().'.pdf"', true);
#zibi
# deprecated  1.5 Use JApplicationWeb instead
# phoca uzywa:
\JFactory::getApplication()->setHeader('Content-type''application/pdf'true);// cache
\JFactory::getApplication()->setHeader('Content-disposition''inline; filename="'.$this->getName().'.pdf"'true);

//Close and output PDF document
return $data;
}

}


Robert_ITMan

With recent upgrades (now on Joomla 3.9.11 and VM 3.6.2) I found I needed to update all my code that directly uses tcpdf as it was moved
from /libraries/tcpdf
to /libraries/vendor/tecnickcom/tcpdf

Notice also /config/lang is no longer included - not required anymore, I commented out the require_once to the /config/lang/eng.php and my PDFs continue to work fine again, mind you I'm not using multiple languages presently. My code:
//load the pdf creator as already installed in libraries
ob_start(); // clear the php buffer needed to stop warning for 'headers already sent'
//require_once( JPATH_BASE . '/libraries/vendor/tecnickcom/tcpdf/config/lang/eng.php' );
require_once( JPATH_BASE . '/libraries/vendor/tecnickcom/tcpdf/tcpdf.php' );
WEBSITES @ OURFINGERTIPS
manage > develop > market > repeat

Save a lot of time and money when focused on building a website that works with marketing efforts to get more leads and sales from visitors.

www.ourfingertips.com