Pages

RSS Feed

Tuesday, December 7, 2010

TCPDF: Getting rid of hidden credit link

I often use PDF export for my web projects: for example for printable product specifications, invoices etc.
The tool of my choice is TCPDF, a php class based on FPDF: it's free and it supports UTF-8 character encoding so you can use it for (nearly) all languages.

But last time I was trying to optimize the pdf output of a php script of mine I was wondering, why there was the font "Helvetica" in the font list shown by Adobe Reader, although I was sure I just used the font "freesans".
After a little while I found the reason: TCPDF adds a credit to itself "Powered by TCPDF (www.tcpdf.org)" on the lower left corner of the last page of each document created (I changed the colour from white to light grey to make it visible):



The www.tcpdf.org part also is a link to the TCPDF website.

Not that I envy TCPDF the fame for the excellent work, but I don't want a customer of my customer to accidently open a new browser window with a website he would never use.

To get rid of that line make a new class that extends TCPDF and override the function Close() (see how nice the author tries to hide the message and the link from us) ;-)


class NOCREDIT extends TCPDF{
   function Close() { //Overwrite to get rid of that tcpdf link
      if ($this->state == 3) {
         return;
      }
      if ($this->page == 0) {
         $this->AddPage();
      }
      $this->lastpage(); 
      // The following lines are responsible for the credit,
      // so I comment them out
      //$this->SetAutoPageBreak(false);
      //$this->x = 0;
      //$this->y = $this->h - (1 / $this->k);
      //$this->lMargin = 0;
      //$this->_out('q');
      //$this->setVisibility('screen');
      //$this->SetFont('helvetica', '', 1);
      //$this->SetTextColor(255, 255, 255);
      //$msg = "\x50\x6f\x77\x65\x72\x65\x64\x20\x62\x79\x20\x54\x43\x50\x44\x46\x20\x28\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29";
      //$lnk = "\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67";
      //$this->Cell(0, 0, $msg, 0, 0, 'L', 0, $lnk, 0, false, 'D', 'B');
      //$this->setVisibility('all');
      //$this->_out('Q');
      $this->endPage();
      $this->_enddoc();
      $this->_destroy(false);
   }
}

TinyMCE: Paste as plain text

I just started using TinyMCE as a WYSIWYG editor for texts in a project and I ran into a problem:

When a user copies some text from an external source such as Open Office into the editor, formatting such as font family, colour, font-size etc. will be preserved even if not intended.

There is a plugin called "paste" that can be configured at the initialization of TinyMCE:

tinyMCE.init({
   ...
   plugins : "paste",
   paste_remove_styles : true,
   paste_retain_style_properties : 'none',
   paste_auto_cleanup_on_paste : true,
   paste_convert_headers_to_strong : false,
   paste_strip_class_attributes : "all",
   paste_remove_spans : true,
   ...
});


But even with all these options set, the text still appears formatted after pasting.

There is also a button called "Paste as plain text"  that does exactly what is intended: no formatting is applied, only the bare naked text is pasted into the editor.
But the user has to click this button before pasting the text, and that's not what I want: I just want text always as plain text.
There should be a way to enable the function on startup...

So I looked in the source code of the plugin and found the flag "pasteAsPlainText" that is set to true when the button gets clicked on. So all we have to do is set this flag after initializing TinyMCE. Fortunately, TinyMCE comes with the callback "oninit" that calls as a named or an anonymous function with the current editor object as argument:

tinyMCE.init({
   ...
   plugins : "paste",
   paste_text_sticky : true,
   oninit: function(ed){
      ed.pasteAsPlainText = true;
   },
   ...
});

You have to add the line "paste_text_sticky : true", because TinyMCE would change the status of "pasteAsPlainText" to false after pasting some text.

Everything's fine now: no more Verdana 16pt in my Arial 12px textarea!
But I got used to the "Paste as plain text" button (called "pastetext" in the button list) so I decided to leave it in my toolbar and show that it is activated on startup, so the user still could deactivate it to paste some formatting intentionally:

tinyMCE.init({
   ...
   theme : "advanced",
   theme_advanced_buttons1 : "bold,italic,pastetext",
   plugins : "paste",
   paste_text_sticky : true,
   oninit: function(ed){
      ed.pasteAsPlainText = true;
      ed.controlManager.setActive("pastetext", true);
   },
   ...
});

So easy!
Hope it helps someone having the same problem!

Things I want to share

Welcome to my new blog!
I never wanted to write a blog, but today I changed my mind.

I was trying to find a special solution for a problem with the JavaScript WYSIWYG editor "TinyMCE" and it took me hours and hours googling around.
Finally I found the solution in the source code so I tried it and it worked!

There are many blogs and forums out there dealing with the same topic, so I could go and sign up at these forums and write my solution there.
Or I just open up a new blog and everytime, I find a rare solution to a common problem I will post it here!

Since I often found solutions to my problem in others blogs, this might be a way to give some of the knowledge I collected back.

Have fun reading!
Feedback will be appreciated :-)