Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: PDF View

PDF View 30 Sep 2013 07:15 #11250

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
  • Karma: 30
I'm stuck with creating a pdf view in a J3 component.
In a J2.5 component, i used docs.joomla.org/J2.5:Creating_PDF_views and it worked very good.
Now in j3, with the exact same code, it doesn't.
Here's another guy having the same problem : stackoverflow.com/questions/15345810/joo...df-from-html-content.
I tried using phoca and other extensions / plugins but with no luck.
I'm pretty sure problem comes from this code : maybe an incompatibility with J3 ?
<?php
/**
 * @package             Joomla.Framework
 * @subpackage  Document
 * @copyright   Copyright (C) 2005 - 2012 Rob Clayburn
 * @license             GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */
 
// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();
 
require_once(JPATH_LIBRARIES .'/joomla/document/html/html.php');
 
/**
 * DocumentPDF class, provides an easy interface to parse and display a pdf document
 *
 * @package             Joomla.Framework
 * @subpackage  Document
 * @since               1.5
 */
class JDocumentpdf extends JDocumentHTML
{
        private $engine = null;
 
        private $name = 'joomla';
 
        /**
         * Class constructore
         * @param       array   $options Associative array of options
         */
 
        function __construct($options = array())
        {
                parent::__construct($options);
 
                //set mime type
                $this->_mime = 'application/pdf';
 
                //set document type
                $this->_type = 'pdf';
 
                if (!$this->iniDomPdf())
                {
                        JError::raiseError(500, 'No PDF lib found');
                }
        }
 
        protected function iniDomPdf()
        {
                $file = JPATH_LIBRARIES .'/dompdf/dompdf_config.inc.php';
                if (!JFile::exists($file))
                {
                        return false;
                }
                if (!defined('DOMPDF_ENABLE_REMOTE'))
                {
                        define('DOMPDF_ENABLE_REMOTE', true);
                }
                //set the font cache directory to Joomla's tmp directory
                $config = JFactory::getConfig();
                if (!defined('DOMPDF_FONT_CACHE'))
                {
                        define('DOMPDF_FONT_CACHE', $config->get('tmp_path'));
                }
                require_once($file);
                // Default settings are a portrait layout with an A4 configuration using millimeters as units
                $this->engine =new DOMPDF();
                return true;
        }
 
        /**
         * Sets the document name
         * @param   string   $name      Document name
         * @return  void
         */
 
        public function setName($name = 'joomla')
        {
                $this->name = $name;
        }
 
        /**
         * Returns the document name
         * @return      string
         */
 
        public function getName()
        {
                return $this->name;
        }
 
        /**
         * Render the document.
         * @access public
         * @param boolean       $cache          If true, cache the output
         * @param array         $params         Associative array of attributes
         * @return      string
         */
 
        function render($cache = false, $params = array())
        {
                $pdf = $this->engine;
                $data = parent::render();
                $this->fullPaths($data);
                //echo $data;exit;
                $pdf->load_html($data);
                $pdf->render();
                $pdf->stream($this->getName() . '.pdf');
                return '';
        }
 
        /**
         * (non-PHPdoc)
         * @see JDocumentHTML::getBuffer()
         */
 
        public function getBuffer($type = null, $name = null, $attribs = array())
        {
                if ($type == 'head' || $type == 'component')
                {
                        return parent::getBuffer($type, $name, $attribs);
                }
                else
                {
                        return '';
                }
        }
 
 
        /**
         * parse relative images a hrefs and style sheets to full paths
         * @param       string  &$data
         */
 
        private function fullPaths(&$data)
        {
                $data = str_replace('xmlns=', 'ns=', $data);
                libxml_use_internal_errors(true);
                try
                {
                        $ok = new SimpleXMLElement($data);
                        if ($ok)
                        {
                                $uri = JUri::getInstance();
                                $base = $uri->getScheme() . '://' . $uri->getHost();
                                $imgs = $ok->xpath('//img');
                                foreach ($imgs as &$img)
                                {
                                        if (!strstr($img['src'], $base))
                                        {
                                                $img['src'] = $base . $img['src'];
                                        }
                                }
                                //links
                                $as = $ok->xpath('//a');
                                foreach ($as as &$a)
                                {
                                        if (!strstr($a['href'], $base))
                                        {
                                                $a['href'] = $base . $a['href'];
                                        }
                                }
 
                                // css files.
                                $links = $ok->xpath('//link');
                                foreach ($links as &$link)
                                {
                                        if ($link['rel'] == 'stylesheet' && !strstr($link['href'], $base))
                                        {
                                                $link['href'] = $base . $link['href'];
                                        }
                                }
                                $data = $ok->asXML();
                        }
                } catch (Exception $err)
                {
                        //oho malformed html - if we are debugging the site then show the errors
                        // otherwise continue, but it may mean that images/css/links are incorrect
                        $errors = libxml_get_errors();
                        if (JDEBUG)
                        {
                                echo "<pre>";print_r($errors);echo "</pre>";
                                exit;
                        } 
                }
 
        }
 
}
Could somebody please help me on this ?
The administrator has disabled public write access.

PDF View 30 Sep 2013 08:08 #11252

  • Tomaselli
  • Tomaselli's Avatar
  • Offline
  • Elite Member
  • Posts: 293
  • Thank you received: 87
  • Karma: 46
I don't know what the problem is without taking a look on your code, but I guess it's related to the view CLASS and functions called by your code. in jcook there are few extra subclasses like CkJView, CkJController, CkJModel (I'm sure you know that).
so I would check the functions involved in your view.pdf.php, one by one.

Personally I use another library to generate my PDF (HTML2FPDF), but it's caused by my needs.
The administrator has disabled public write access.

PDF View 30 Sep 2013 08:12 #11253

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
  • Karma: 30
I would use anything that works, including HTML2FPDF.
I understand what you mean by the class and functions, but using template=>component or template=>system should avoid those problems if i understood it right.
You think it's more related to the cook component than to the code to create pdf in joomla that would be incompatible with J3 ?
Thanks for your help anyway
The administrator has disabled public write access.

PDF View 30 Sep 2013 08:18 #11254

  • Tomaselli
  • Tomaselli's Avatar
  • Offline
  • Elite Member
  • Posts: 293
  • Thank you received: 87
  • Karma: 46
I'm still sleeping :lol: , I didn't see you also posted the code...I'll take a look on what you posted, surely would be better to do few tests in the real life world of your component....but maybe I'm enough lucky to find where the problem is
The administrator has disabled public write access.

PDF View 30 Sep 2013 08:27 #11255

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
  • Karma: 30
vecrea[at]gmail.com : if you want access, send me a mail and i'll give you details.

Still i would think it would be a HUGE addition to cook to have a pdf button.
The administrator has disabled public write access.

PDF View 30 Sep 2013 09:03 #11256

  • Tomaselli
  • Tomaselli's Avatar
  • Offline
  • Elite Member
  • Posts: 293
  • Thank you received: 87
  • Karma: 46
if you just want it solved without other pain, you could add my way to generate PDF, maybe the Admin could also add this functionality in j-cook, it's pretty simple but effective!
the reason why I'm not using the default joomla pdf library, is because HTML2FPDF mPDF is more fit for my needs, it's simple, and works very good.

these are the main steps, so you may need other modifications on your component, for example if you need to install it automatically at the component installation process you have to remember to add few lines also in the component.xml, by the way:
  1. add the PDF library to your component
  2. add some other custom files and ONE function to your helper
  3. modify the view.html.php of your view to print

1) download the PDF library and add the files into a subfolder in your administrator\components\com_mycomponent\helpers
download the library.

2) add the following function into your MycomponentHelper class in helper.php:
	public static function generatePdf($html, $download = false){
		$mpdf_library = JPATH_ADMINISTRATOR .DS.'components'.DS.'com_mycomonent'.DS.'helpers'.DS.'mpdf'.DS.'mpdf.php';

		if(file_exists($mpdf_library)){
			require_once($mpdf_library);
		}
		
		$mpdf=new mPDF();

		// add CSS files for better rendering
		$css_files = array();

		// some css needed
		$css_files[] = JPATH_SITE .DS.'components'.DS.'com_mycomponent'.DS.'css'.DS.'my_component.css';
		$css_files[] = JPATH_SITE .DS.'components'.DS.'com_mycomponent'.DS.'css'.DS.'pdf.css';

		foreach($css_files as $css){
			if(file_exists($css)){
				$stylesheet = '<style>'. file_get_contents($css) .'</style>';
				$mpdf->WriteHTML($stylesheet,1);
			}
		}
		
		$mpdf->setFooter('{PAGENO} / {nb}');
		$mpdf->WriteHTML($html);

		if($download){
			$mpdf->Output('invoice.pdf', 'D');
			return;
		}
		
		$content = $mpdf->Output('', 'S');
		return $content;
	}


3) in your display function in view.html.php add the following lines (put them where you think it is better for your needs in the code):
		$jinput = new JInput;
		$render = $jinput->get('render', null, 'CMD');

and then before the end of the display function add this:
		if($render == 'pdf'){
			$this->pdf = true;

			ob_start();
			parent::display($tpl);
			$output = ob_get_contents();
			ob_end_clean();
			MycomponentHelper::generatePdf($output, true);
			jexit();
		}

DONE!!! isn't simple?!

now you add the "render=pdf" in the link to that view and you'll download the pdf.
Surely this is not what JOOMLA standard coding wants, but personally I do not care, this code is fast, clean and perfectly working.
Last Edit: 30 Sep 2013 12:56 by Tomaselli.
The administrator has disabled public write access.
The following user(s) said Thank You: twev, ewajoom

PDF View 30 Sep 2013 11:16 #11257

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
To this question I can answer because I am doing the PDF integration right now.
Yesterday it has worked nice for the first time with images, and I keep going to offer you the feature when it works well.

My investigations :

PDF lib has been removed from Joomla 3. I don't know the reason.
In past I have done a project with FPDF, but I am now using : DomPdf
pxd.me/dompdf/www/examples.php

Very good, and convert HTML, so for Cook integration it is perfect.
Also FPDF is very limited to rotate text and many others limitations.

I tried the example code given on Joomla documentation. But it does not work in fact because it calls the document instead of the view.
For the moment I am not using JDocument.

When you set format=pdf in url, Joomla is searching for view.pdf.php (Your view file)
Simply create a super PDF VIEW class inherinting XxxxClassView

In this super class, redefine display() in order to catch the generated template, and send it to render to domPdf.

Then, the images should not be loaded indirectly, but from direct url if you want it to work.
Because domPdf is retreiving automatically the real physical path on hdd.
It retreives also CSS physical files in order to parse them. (Not tested css for the moment)

Today I am gonna rewrite the way to acces files. Directory markers will stay the same, but more convenient to play with files :
- Indirect access (through controller)
- Access by direct url
- Retrieve physical file on hdd.

For the moment still searching to make it working.

Another issue I have encountered, you must have a container markup (div) for the whole template result because dompdf seems to decode only the first XML node (HTML in our case)
If you do not transform your native generated template, an error will occur :
" Error : Extra content at the end of the document"

Hope it helps. This new feature will not be available tomorrow. I must do it first and then test it for months before to propose you in Cook.
Coding is now a piece of cake
Last Edit: 30 Sep 2013 11:26 by admin.
The administrator has disabled public write access.

PDF View 30 Sep 2013 11:58 #11258

  • Tomaselli
  • Tomaselli's Avatar
  • Offline
  • Elite Member
  • Posts: 293
  • Thank you received: 87
  • Karma: 46
HTML2FPDF is an improved FPDF version THE LIBRARY I'M USING IS mPDF, it works very well to me, and it always gives an output even if the HTML/CSS is not well coded (many other libraries give a blank page, any problem stops the engine to generate the pdf).This is my experience.

I didn't try the DomPdf because it looks to me an abandoned project, there is no updated coded from 09 / 02 / 2012. I know this doesn't mean anything, but I prefer fresh and mantained code, in my experience, many times this means better performances and more cross compatibility, and after I tried few pdf php libraries, I found HTML2FPDF mPDF seems the best.....considering not only the features but also, performances, easyness on coding, compatibility with real world of HTML/CSS pages in joomla.

Then you know what the best would be for your son jcook :)
Last Edit: 30 Sep 2013 12:57 by Tomaselli.
The administrator has disabled public write access.
The following user(s) said Thank You: VeCrea

PDF View 30 Sep 2013 11:59 #11259

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
  • Karma: 30
Still not working for me though, still blank page
The administrator has disabled public write access.

PDF View 30 Sep 2013 12:02 #11260

  • Tomaselli
  • Tomaselli's Avatar
  • Offline
  • Elite Member
  • Posts: 293
  • Thank you received: 87
  • Karma: 46
did you try my trick?
The administrator has disabled public write access.

PDF View 30 Sep 2013 12:03 #11261

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
  • Karma: 30
I certainly tried... but i believe i didn't do a good job...
The administrator has disabled public write access.

PDF View 30 Sep 2013 12:04 #11262

  • Tomaselli
  • Tomaselli's Avatar
  • Offline
  • Elite Member
  • Posts: 293
  • Thank you received: 87
  • Karma: 46
I'll check it with the access you gave me
The administrator has disabled public write access.

PDF View 30 Sep 2013 12:05 #11263

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
  • Karma: 30
Thanks mate
The administrator has disabled public write access.

PDF View 30 Sep 2013 12:20 #11264

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Thanks for the advice I will check also HTML2FPDF.
Anyway the rendering lib is just the final part. Cook can introduce both possibility or a choice.

Cook must handle all that, and the builder interface too.
Thanks to everybody for this nice and very usefull conversation.

By the way, I am also beginning to add new controls such as jPlayer for MP3, etc...
Maps, calendars... What are your favorites controls you would like to see in Cook.
jQuery UI or not ?

Now Cook Generator runs on a powerfull framework.
New Controls, APIs, features can be added. At least for presentation of datas.

POLL : WHAT ARE YOUR FAVORITES CONTROLS AND RENDERERS, UI, ...
Coding is now a piece of cake
Last Edit: 30 Sep 2013 12:24 by admin.
The administrator has disabled public write access.

PDF View 30 Sep 2013 12:59 #11265

  • Tomaselli
  • Tomaselli's Avatar
  • Offline
  • Elite Member
  • Posts: 293
  • Thank you received: 87
  • Karma: 46
I tried so many PDF libraries I made the mistake to name the wrong one!!!! :lol: :lol:

I USED the mPDF library
Last Edit: 30 Sep 2013 12:59 by Tomaselli.
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Time to create page: 0.142 seconds

Get Started