Brand PDF before it can be downloaded

  • Posts: 45
  • Thank you received: 2
13 years 1 week ago #30146

I will sell PDFs with my shop products. I would like to have them branded with the customer's username, so I wrote this little PHP code:

$document = "test.pdf";
$branding = "Licensed for L. E. Bogen";
brandPDF($document, $branding);

function brandPDF($document, $branding) {
	require_once 'Zend/Pdf.php';
	$pdf = Zend_Pdf::load($document);
	$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
	foreach ($pdf->pages as &$page) {
		$page->setFont($font, 12);
		$page->drawText($branding, 10, 10);
	}
	$pdf->save('Branded_' . $document);
}

How can I include this in Hikashop deftly, so that a customer gets his branded PDF instead of the original file?

And additionally a probably more PHP affine question:
Very nice would be, if I could relinquish the "save"-function. But I think a download won't be possible without having the file saved first, right?

Last edit: 13 years 1 week ago by klimmbimm.

Please Log in or Create an account to join the conversation.

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
13 years 1 week ago #30203

Hi,

You can add your code in the function sendFile of the file administrator/components/com_hikashop/classes/file.php

As this is a direct modification of a core file of HikaShop, you will loose the modification when you update HikaShop so please make sure that you backup your modification.

Please Log in or Create an account to join the conversation.

  • Posts: 45
  • Thank you received: 2
13 years 1 week ago #30270

nicolas wrote:

Hi,

You can add your code in the function sendFile of the file administrator/components/com_hikashop/classes/file.php

As this is a direct modification of a core file of HikaShop, you will loose the modification when you update HikaShop so please make sure that you backup your modification.


thanks for this information!
do you think the feature is nice enough to be build in in hikashop in some later version? (is there a form for feature requests?)

Please Log in or Create an account to join the conversation.

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
13 years 1 week ago #30298

So far no one else requested such feature. Also, it is really specific to your case and require the zend Pdf library which should be added in HikaShop for that. So I don't think that we would add it.

However, it's a nice idea to trigger hikashop plugins when calling that function so that you can dynamically do some processing on it in a plugin, instead of having to modify a core file. We'll look at adding that in the near future.

Please Log in or Create an account to join the conversation.

  • Posts: 45
  • Thank you received: 2
13 years 1 week ago #30421

If I would try to put my code in a (hikashop-) plugin, what would I have to do?
Could you recommend me any easy instruction for this?

Please Log in or Create an account to join the conversation.

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
13 years 1 week ago #30432

First, for next release we'll add the code below at the beginning of that sendFile function:
JPluginHelper::importPlugin( 'hikashop' );
$dispatcher =& JDispatcher::getInstance();
$do = true;
$dispatcher->trigger( 'onBeforeDownloadFile', array( & $file, & $do) );
if(!$do) return false;

that's what will trigger the plugins.

Then, you can create a hikashop plugin, like the plugins which already exist in the folder /plugins/hikashop/ of your website and implement the function onBeforeDownloadFile and put your code there.

Please Log in or Create an account to join the conversation.

  • Posts: 45
  • Thank you received: 2
13 years 5 days ago #30851

thank you, this works great and was fairly easy.

this is my plugin code:

branding.xml
see: pastebin.com/9iuNE4SC for better readability

branding.php
see Pastebin - main part of a Hikashop plugin for branding sold PDFs
for better readability

A few more things:
- I wasn't in the mood to deal with GNU etc. So I removed author and licensing lines from the files. Feel free to use, modify and improve the code. But if you improve, please let me know. ;-)
- I finally included a reduced Zend Framework for PDF editing in the plugin. So it hasn't to be "installed" seperately.
- What for is the $do variable?

Last edit: 12 years 10 months ago by klimmbimm. Reason: improved code

Please Log in or Create an account to join the conversation.

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
12 years 11 months ago #31365

Thank you for sharing your solution.

The $do variable allows you to cancel the download of the file by setting it to false.

Please Log in or Create an account to join the conversation.

  • Posts: 21
  • Thank you received: 1
11 years 2 months ago #121179

Does this plugin exist as an easy to install solution? If so, can you please share the plg_.zip file?
thanks

Please Log in or Create an account to join the conversation.

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
11 years 2 months ago #121281

It's simple. You get the text of branding.xml and branding.php from pastebin and add it in files of the same name. Then, you zip both files together and you will get your installable zip.

Please Log in or Create an account to join the conversation.

  • Posts: 21
  • Thank you received: 1
11 years 2 months ago #121413

Thanks for answer, the only problem I face right now is, that it is made for joomla 1.5 - will it work also for joomla 2.5? If not, do you have any idea how to implement this great feature into my hikashop?
thanks

The following user(s) said Thank You: klimmbimm

Please Log in or Create an account to join the conversation.

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
11 years 2 months ago #121513

If you want to make it work for joomla 2.5, it requires a few changes in the xml file. I've done it for you:
pastebin.com/WgCvKNEK

Please Log in or Create an account to join the conversation.

  • Posts: 21
  • Thank you received: 1
11 years 2 months ago #121547

I am sorry to say that, but I get error during instalation (index.html does not exist) - is there any bug?

thanks a lot for help

Please Log in or Create an account to join the conversation.

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
11 years 2 months ago #121567

Ah yes, I read the code a bit more. You'll need empty index.html, README_DE and CHANGELOG files in the package, the font calibri.ttf, and the zend framework files in a Zend folder as listed in the xml file.
So it's more complex to do than what I thought first.

The best would be that @klimmbimm provides a zip of what he has done.

Please Log in or Create an account to join the conversation.

  • Posts: 45
  • Thank you received: 2
10 years 7 months ago #151739

I am very sorry for the late answer. I was doing a lot of other stuff meanwhile. Tomorrow I hope to get access to a installation zip file, which I will upload then.

Best,

KB


EDIT: Found the zip and merged nicolas' changes to make it officially compatible with Joomla 2.5.

File Attachment:

File Name: h36202c5.zip
File Size:832 KB


To be honest, I didn't share the zip in the past, because I didn't (and still don't) have a clue about copyright stuff. E.g. I am using part of the Zend Framework to write to PDFs and the calibri.ttf.

REGARDING THE SUPPORTED JOOMLA VERSION: I am using the plugin with Joomla 2.5. By now, without the changes made by nicolas (thank you!). I guess, it would run under Joomla 3.x as well. Feel free to try and let us know.
REGARDING THE MISSING INDEX.HTML: It's like nicolas mentioned before. You need additional files. But you could remove at least calibri.ttf from the listing in the branding.xml file, if you like to fiddle around a bit. :)
Please take care to comment or remove the following line in branding.php
$font = Zend_Pdf_Font::fontWithPath(JPATH_BASE . DS . 'plugins' .DS . 'hikashop' . DS . 'branding' . DS . 'calibri.ttf'); // works, if file calibri.ttf is supplied
and uncomment the prepared line above:
//$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA); // works without having a TTF font file put in place

Attachments:
Last edit: 10 years 7 months ago by klimmbimm.
The following user(s) said Thank You: Xavier

Please Log in or Create an account to join the conversation.

Time to create page: 0.109 seconds
Powered by Kunena Forum