Beyond Stamping PDFs with Custom Fonts
If you have tried to use the included font uploader in WaterWoo or EDDiMark, most the time you will be satisfied. It makes it very easy to add and watermark PDFs in nearly any font.
Unfortunately for some users whose languages require special characters, those characters might not be mapped in the font definition files and the dreaded ??????? or ▯▯▯▯▯▯▯ will show. That’s stressful!
TCPDF sometimes requires some extra massaging to get those characters to show correctly. If your chosen font supports those characters, but yet is an extended font (meaning it supports very many characters in several languages), TCPDF might be truncating (subsetting) and omitting extended characters to try to speed PHP processing. Embedding fonts with a PDF is very expensive CPU-wise, so the first thing to try is to find the font most specific to your purpose, without unnecessary characters. Then we must bypass the built-in WaterWoo/EDDiMark uploader and manually install the font, to avoid subsetting.
Here is a way to do it so that your chosen font will be a dropdown selectable in the WaterWoo settings (making it easier to manage in the future). It involves an extra step, some work to create the font files “manually” (not really manually, but less automatically). If you know how to download files, rename files, and/or change file contents, you can do this yourself.
- Take the TTF font you want to use and upload it using the WaterWoo font uploader. (You might have already done this and realized it didn’t work for you, but in that process TCPDF addTTFfont() has run, creating three files which we are going to overwrite). Let’s find those files.
- Go to the /wp-content/uploads/waterwoo-pdf/fonts/ folder for your WordPress site and find the (usually 3) files created there under your font name. The file names should be pretty similar to the name of the font you have uploaded, and will end with “.z” “.ctz.z” and “.php”. Those are the files you will need to re-build another way. Don’t do anything with them yet. Just know where they are.
- Now go to http://fonts.snm-portal.com/* and upload your prompt TTF file to the box upper left. Three new files will be created for you: promptlight.php, promptlight.z, and promptlight.ctg.z. To access them you must first check the “I have rights to convert this font” checkbox. If it doesn’t seem to work, try another browser. I have success with Firefox. If you click the PHP file it will likely take you to a webpage. You don’t want that, unless you’re familiar with PHP code. Instead, right click it and “save link as.” You now have three files which contain the file contents you want. They are not subsetted, and that is good.
Again, if they are named slightly different, that is OK. You need the CONTENTS of these three files.
- For each of these three files, replace the contents of the files you found in step 2 with the contents you just created, keeping the same file name, and making sure it’s all at /wp-content/uploads/waterwoo-pdf/fonts. The other option is to rename the new files with the names of the old files (and delete/replace the old files). These font definition files will now have much more content because they are not truncated (subsetted). Subsetting is done by TCPDF to reduce PDF size and speed up downloads. This font work you are doing WILL make your PDFs heavier, so the smaller size font file you can use (with less unneeded/unused characters from other languages) the better. Keep that in mind if you get PHP timeouts. The author of TCPDF discusses fonts and performance here.
Your watermarking should now work. If you need to make further changes with font encoding, you may have to alter the core TCPDF library. However, WaterWoo and EDDiMark have included several filter hooks for adjusting your font handling, hopefully making that need less likely.
* If the SNM-Portal font converter doesn’t work for you, it’s possible your font file is too large. Another service to try is XML-Convert. If you still don’t have luck, PHP is still there for you! You can run a one-time call to TCPDF addTTFfont() using Code Snippets or through your theme functions.php file, and this will add the font to your WaterWoo installation (by default, successfully installed fonts will be found inside the wp-content/uploads/waterwoo-pdf/fonts folder).
WWPDF_TCPDF_FONTS::addTTFfont( WWPDF_PLUGIN_DIR . 'full/path/to/your/ttf/font', 'TrueTypeUnicode' );
Your code would look a little like this. Make sure to add WWPDF_ to the class name as shown above. Learn more about how to use the addTTFfont() method.