PDF Plugin for custom component

Phoca PDF - creating PDF documents in Joomla! CMS
netamity
Phoca Newbie
Phoca Newbie
Posts: 5
Joined: 30 Apr 2020, 13:17

PDF Plugin for custom component

Post by netamity »

How can I create a PhocaPDF plugin for a custom component?
I've taken a look at the code for the content plugin, and can see onBeforeCreatePDFContent and onBeforeDisplayPDFContent and think I can adapt these to pull data from my component.
The issue is, if I create a new plugin how do I get PhocaPDF to know to use it if i add ?tmpl=component&format=pdf to the URL. If I have index.php?option=com_sea&view=messages&layout=report&tmpl=component&format=pdf (com_sea is my custom component)

If i try that link now I get 'Error. Failed to load PDF document.' Not sure if it means it cant find a plugin for that component or whether its trying to use the content plugin and can't get a valid content item.

Thanks for your help. What would be really great is if I could get a 'hello world' type plugin that a could associate with my component that would just spit out a pdf document with static content, and then I could recode it to pull in the dynamic data.

Many thanks
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 47794
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: PDF Plugin for custom component

Post by Jan »

Hi, first, your component needs to have the pdf view, see examples e.g. in Phoca Cart:

Image which should just echo the output

The PDF view just calls the PDF document in Joomla! (which was added by Phoca Cart) and this document then use the plugin to render the page.

Jan
If you find Phoca extensions useful, please support the project
netamity
Phoca Newbie
Phoca Newbie
Posts: 5
Joined: 30 Apr 2020, 13:17

Re: PDF Plugin for custom component

Post by netamity »

I have added a view.pdf.php file to the correct folder. I used the one in com_content as an example and now have:
defined('_JEXEC') or die;
jimport('joomla.application.component.view');

class SeaCkViewMessages extends JViewLegacy
{


function display($tpl = null)
{
$app = JFactory::getApplication();
$user = JFactory::getUser();

//die('works');
$document = JFactory::getDocument();
$document->setHeader('header');

echo 'lots of text here';
$document->setArticleText('lots of text here');
$document->setArticleTitle('test');

}
}
I'm still getting the same 'Error- Failed to load PDF document.' as I did before. Do I need to create a plugin for my component as well? If so, how is this done?
Thanks for your help.
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 47794
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: PDF Plugin for custom component

Post by Jan »

Hi, yes, each component needs own plugin. Just see e.g. as example the Phoca PDF Phoca Cart Plugin:
https://www.phoca.cz/download/category/ ... art-plugin

Jan
If you find Phoca extensions useful, please support the project
netamity
Phoca Newbie
Phoca Newbie
Posts: 5
Joined: 30 Apr 2020, 13:17

Re: PDF Plugin for custom component

Post by netamity »

Hi Jan,
I have edited the phocacart plugin to create one for my component, installed and activated. I still get the error 'Error- Failed to load PDF document', so it's not finding the plugin by the looks of it.
If my component is called com_sea, I assume my plugin class should be plgPhocaPDFSea and use triggers such as onBeforeCreatePDFSea and onBeforeDisplayPDFSea? Does the pdf document call these based on the name of the plugin or component (because they're not defined anywhere else)?
I'm obviously still not clear on how the pdf document knows which plugin to call. I was hoping I could get it working through renaming along the lines of phocacart example, but it's not working.
Is there any way to see what the problem is- ie. what its looking for and not finding? I grepped to find 'Failed to load PDF document'' but couldn't find it. I was hoping I could track down there this error is fired so that i could work my way backwards.
Any help appreciated!
--------
Ah, I think I have a lead. I've installed the plugin and activated it in the plugin manager, but when viewing it in the Phoca PDF component, it says 'Plugin does not exist'. What have I done wrong here?
--------
I added the default_sea.php templates to administrator/components/com_phocapdf/views/phocapdfplugins/tmpl and administrator/components/com_phocapdf/views/phocapdfplugin/tmpl and saved. It works fine, but still the same 'Error- Failed to load PDF document' when trying to use the plugin in my component, so that wasn't the issue sadly.
-------
Andy
netamity
Phoca Newbie
Phoca Newbie
Posts: 5
Joined: 30 Apr 2020, 13:17

Re: PDF Plugin for custom component

Post by netamity »

Hi Jan,
I'm discovering more file modifications that look like they're required, but still getting the same error message.
administrator/components/com_phocapdf/helpers/phocapdfrenderer.php looks like it's the link between com_phocapdf and the plugin that I've created. I now have a new switch entry:

Code: Select all

case 'com_sea':

				//die('ok then');
				// Get info from Phoca PDF Phoca Cart Plugin
				$content 	= new JObject();

				JPluginHelper::importPlugin('phocapdf');

				$results 	= \JFactory::getApplication()->triggerEvent('onBeforeCreatePDFSea', array (&$content, $staticData));

				if (JFile::exists(JPATH_SITE.'/plugins/phocapdf/sea/sea.php')) {
					require_once(JPATH_SITE.'/plugins/phocapdf/sea/sea.php');
					$pdf = new PhocaPDFSeaTCPDF($content->page_orientation, 'mm', $content->page_format, true, 'UTF-8', $content->use_cache);
				} else {

					throw new Exception('Document cannot be created - Loading of Phoca PDF Plugin (Sea) failed', 500);
					return false;
				}

			break;
and later

Code: Select all

case 'com_sea':
				$results = \JFactory::getApplication()->triggerEvent('onBeforeDisplayPDFSea', array (&$pdf, &$content, &$document, $staticData));
			break;
I was hoping that would fix the error or at least give me a new one. I realise the error message 'Failed to load PDF document.' is from the Chrome PDF viewer, but it shows there's an issue creating the pdf document.
How to get to the bottom of this? Is there a place to disable the application/pdf headers so that I can view some output in my browser? What do you suggest?

Thanks so much for this. I think if there were clear guidelines for creating plugins for your PDF extension it'd be massively useful, as creating PDFs from html seems like an improbably difficult thing to do

Any help is appreciated. Many thanks.
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 47794
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: PDF Plugin for custom component

Post by Jan »

Hi, "failed to load PDF document" can be caused by errors rendered in the document.

Which means:

- before rendering the PDF, you need to debug it and be 100%, it does not produce any PHP errors
- this mostly means, you will just exit the script before it renders teh PDF headers and check if PHP displays some errors
- if error reporting is set to ON and PHP errors are displayed on the site, such can be displayed inside PDF - so instead of displaying the PDF in browser, you just download it and with help of text editor you will see its source code - there can be listed the server errors


Yes, producing PDF is not so easy
- not only, that the plugin needs to work correctly
- even the component needs to prepare right code (text) for the output
- mostly IDE program like Eclipse is needed so you will debug which function calls which function and you will see the flow so you can debug the output on right place.

Mostly, you will create all functions which produce the output - in text form, such you will debug so you know, there are no errors - if everything is OK, then you will start on producing PDF ... and with reverse behaviour- first you will produce some simple text, to see that the PDF feature is OK, then you will start to implement output from your component where you know, the output is without errors.

Jan
If you find Phoca extensions useful, please support the project
netamity
Phoca Newbie
Phoca Newbie
Posts: 5
Joined: 30 Apr 2020, 13:17

Re: PDF Plugin for custom component

Post by netamity »

Hi Jan,
Thank you for explaining. Can i ask for two more things?
1) What is the best way to output the content in a web page - exit the script as you suggest (instead of applying the pdf headers). What file and changes should I edit to do this? As soon as I can see the php errors I can fix them- the problem at the moment is that I can't track it down because I have no access to the errors.
2) My approach would definitely be to output 'Hello world' into a pdf document if I could get that working, and then work backwards to generate the function to create the content (and have the option to exit the header script as in item 1 to debug any php errors). I've tried adding static text to variables that I imagined were the final output string (in the view.pdf.php document) but I still get the error. What is the final point for this content string where i can insert 'Hello world' for the time being in order to get a pdf to output?

If you were able to provide specific file and line/function details to (1) Exit the headers and output in the browser, and (2) Set the content as 'Hellow world' just before the pdf library generates a pdf document from it, that would be very much appreciated
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 47794
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: PDF Plugin for custom component

Post by Jan »

Hi,

1) you can exit the script everytime on every place before sending data to TCPDF library but of course you need to follow the function flow. So this can be in every place - in your component, then in the Phoca PDF plugin of this component and even in Phoca PDF component. Mostly it happens in the plugin.

2) In fact there is no specific file to debug, it just can happens everywhere. If you get some error, you need to stop the script before this error. Mostly, errors which are debugged, can get debug trace - so you see all files from start to the part of the error and you can debug in all listed files - before the error happens.

It is really hard to give some clue advice regarding errors - you need to always find some good debug tool (Joomla! debug is great, it display the debug trace) and debug before the error happens. :idea:

Jan
If you find Phoca extensions useful, please support the project
Post Reply