mpdf can't add new font-family helvetica-neue-condensed-black - php
I'm using mPdf version 6 and want to add new custom font in it but it's not using new font family. What I did is:
mpdf\ttfonts folder
in config_fonts.php file added following code in fontdata:
"helvetica" => array(
'R' => "helvetica-neue-condensed-black-59233f88d5395.ttf",
),
where "helvetica-neue-condensed-black-59233f88d5395.ttf" is the name of the file.
Following is my mPdf code for creating pdf:
$mpdf = new \Mpdf(['mode' => 'c']);
$mpdf->SetFont('helvetica');
$mpdf->SetTitle('PDF title');
$mpdf->AddPage('P','','','','',8,8,5,5,10,10);
$mpdf->WriteHTML($this->html);
$mpdf->Output('test.pdf', 'd');
I checked whatever I write in SetFont nothing in Pdf changed..
As from the mpdf docs
Core non-embedded fontsPermalink
PDF files have certain standard fonts: Helvetica, Times and Courier in
the win-1252 character set, and Zapfdingbats and Symbol character
sets. These fonts should be available to any PDF reading program, and
do not need to be embedded in the PDF document.
Advantages: Small file size, fast processing, small memory usage.
Disadvantages: Limited choice of fonts for appearance. Will not
display characters which are not in the win-1252 Symbols, or Dingbats
codepages (suitable for most Western European languages).
To use core fonts only, use 'c' for the mode configuration key:
$mpdf = new \Mpdf\Mpdf(['mode' => 'c']);
Change the mode from c to something else.
Related
Can't import most TTF files to TCPDF
I have read all the tutorials there are to read about this topic, I simply can't make this work, neither do the example codes and online tools... When I try to import most .ttf files into TCPDF fonts, the resulting php file goes something like this: <?php $type='TrueType'; $name='FontAwesome5Free-Solid'; $desc=array('Ascent'=>875,'Descent'=>-125,'CapHeight'=>875,'Flags'=>96,'FontBBox'=>'[5000 5000 -5000 -5000]','ItalicAngle'=>-50,'StemV'=>70,'MissingWidth'=>1000); $up=-123; $ut=49; $dw=1000; $cw=array( 0=>1000,1=>1000,2=>1000,3=>1000,4=>1000,5=>1000,6=>1000,7=>1000,8=>1000,9=>1000, 10=>1000,11=>1000,12=>1000,13=>1000,14=>1000,15=>1000,16=>1000,17=>1000,18=>1000,19=>1000, 20=>1000,21=>1000,22=>1000,23=>1000,24=>1000,25=>1000,26=>1000,27=>1000,28=>1000,29=>1000, 30=>1000,31=>1000,32=>1000,33=>1000,34=>1000,35=>1000,36=>1000,37=>1000,38=>1000,39=>1000, 40=>1000,41=>1000,42=>1000,43=>1000,44=>1000,45=>1000,46=>1000,47=>1000,48=>1000,49=>1000, 50=>1000,51=>1000,52=>1000,53=>1000,54=>1000,55=>1000,56=>1000,57=>1000,58=>1000,59=>1000, 60=>1000,61=>1000,62=>1000,63=>1000,64=>1000,65=>1000,66=>1000,67=>1000,68=>1000,69=>1000, 70=>1000,71=>1000,72=>1000,73=>1000,74=>1000,75=>1000,76=>1000,77=>1000,78=>1000,79=>1000, 80=>1000,81=>1000,82=>1000,83=>1000,84=>1000,85=>1000,86=>1000,87=>1000,88=>1000,89=>1000, 90=>1000,91=>1000,92=>1000,93=>1000,94=>1000,95=>1000,96=>1000,97=>1000,98=>1000,99=>1000, 100=>1000,101=>1000,102=>1000,103=>1000,104=>1000,105=>1000,106=>1000,107=>1000,108=>1000,109=>1000, 110=>1000,111=>1000,112=>1000,113=>1000,114=>1000,115=>1000,116=>1000,117=>1000,118=>1000,119=>1000, 120=>1000,121=>1000,122=>1000,123=>1000,124=>1000,125=>1000,126=>1000,127=>1000,128=>1000,129=>1000, 130=>1000,131=>1000,132=>1000,133=>1000,134=>1000,135=>1000,136=>1000,137=>1000,138=>1000,139=>1000, 140=>1000,141=>1000,142=>1000,143=>1000,144=>1000,145=>1000,146=>1000,147=>1000,148=>1000,149=>1000, 150=>1000,151=>1000,152=>1000,153=>1000,154=>1000,155=>1000,156=>1000,157=>1000,158=>1000,159=>1000, 160=>1000,161=>1000,162=>1000,163=>1000,164=>1000,165=>1000,166=>1000,167=>1000,168=>1000,169=>1000, 170=>1000,171=>1000,172=>1000,173=>1000,174=>1000,175=>1000,176=>1000,177=>1000,178=>1000,179=>1000, 180=>1000,181=>1000,182=>1000,183=>1000,184=>1000,185=>1000,186=>1000,187=>1000,188=>1000,189=>1000, 190=>1000,191=>1000,192=>1000,193=>1000,194=>1000,195=>1000,196=>1000,197=>1000,198=>1000,199=>1000, 200=>1000,201=>1000,202=>1000,203=>1000,204=>1000,205=>1000,206=>1000,207=>1000,208=>1000,209=>1000, 210=>1000,211=>1000,212=>1000,213=>1000,214=>1000,215=>1000,216=>1000,217=>1000,218=>1000,219=>1000, 220=>1000,221=>1000,222=>1000,223=>1000,224=>1000,225=>1000,226=>1000,227=>1000,228=>1000,229=>1000, 230=>1000,231=>1000,232=>1000,233=>1000,234=>1000,235=>1000,236=>1000,237=>1000,238=>1000,239=>1000, 240=>1000,241=>1000,242=>1000,243=>1000,244=>1000,245=>1000,246=>1000,247=>1000,248=>1000,249=>1000, 250=>1000,251=>1000,252=>1000,253=>1000,254=>1000,255=>1000); $enc='cp1252'; $diff=''; $file='lol.z'; $originalsize=191836; // --- EOF --- Look that all characters are invalid, basically. This is the result I get using my PHP source, or using online converters made for TCPDF (eg.: http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf or http://fonts.snm-portal.com/). I am trying to add font-awesome's TTF files (eg. fa-solid-900.ttf) to my TCPDF, but I get this error. I have no idea what to do now.
You need to import FontAwesome as a Unicode font. [Also noting: Characters 0-255 will have character widths of 1000 in your example because they don't exist in this font and 1000 is the width defined for your missing glyph width.] For your other fonts that are failing, try importing them as Unicode fonts as well or with a different encoding table. //Ignore the warnings about undefined indexes on first import. //There's no H or x glyphs in the font, so the warning is expected. $fa = TCPDF_FONTS::addTTFfont('fa-solid-900.ttf', 'TrueTypeUnicode', '', 4); //Note: using empty string for $enc since this is a Unicode font //Note: using 4 for $flags to signify a symbol font. 32 for text fonts. $pdf->setFont($fa); $pdf->writeHTMLCell(100,120,40,10,""); //An example inlined with normal text: $pdf->setFont('helvetica'); $pdf->writeHTMLCell(100,120,40,20,"Should be a gamepad: <span style=\"font-family:$fa;\"></span>"); Attached screenshot of test output results: Test script: <?php //Update to your TCPDF path include 'TCPDF/tcpdf.php'; $pdf = new TCPDF('P', 'mm','Letter', true, 'UTF-8', false, true); $pdf->addPage(); $pdf->setFontSubsetting(true); $fa = TCPDF_FONTS::addTTFfont('fa-solid-900.ttf', 'TrueTypeUnicode', '', 4); $testtext = "Font name: {$fa}<br>". "Should be a gamepad: <span style=\"font-family:$fa;\"></span>"; $pdf->writeHTMLCell(100,10,10,10,$testtext); $pdf->Output(dirname(__FILE__).'/fasolid-test.pdf', 'F');
ImageMagick with PHP text overflowing PDF to JPG conversion
I'm trying now to convert a PDF file to JPG, using ImageMagick with PHP and CakePHP. The PDF is in perfect shape and it's right the way it should be, but the image generated from the PDF is always overflowing the borders of the file. Until now, I've tried tweaking the code for the generation with no sucess, reading a lot from the PHP docs (http://php.net/manual/pt_BR/book.imagick.php). Here are the convertion code: $image = new Imagick(); $image->setResolution(300,300); $image->setBackgroundColor('white'); $image->readImage($workfile); $image->setGravity(Imagick::GRAVITY_CENTER); $image->setOption('pdf:fit-to-page',true); $image->setImageFormat('jpeg'); $image->setImageCompression(imagick::COMPRESSION_JPEG); $image->setImageCompressionQuality(60); $image->scaleImage(1200,1200, true); $image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN); $image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE); $image->writeImage(WWW_ROOT . 'files' . DS . 'Snapshots' . DS . $filename); Here are the results: https://imgur.com/a/ISBmDMv The first image is the PDF before the conversion and the second one, the image generated from the PDF where the right side text overflows. So, why this is happening? And if someone got some alternative for any tech used (the GhostScript, ImageMagick, etc) is also welcome! Thanks everyone!
Its very hard to say why you see the result you do, without seeing the original PDF file, rather than a picture of it. The most likely explanation is that your original PDF file uses a font, but does not embed that font in the PDF. When Ghostscript comes to render it to an image it must then substitute 'something' in place of the missing font. If the metrics (eg spacing) of the substituted font do not match precisely the metrics of the missing font, then the rendered text will be misplaced/incorrectly sized. Of course since its not using the same font it also won't match the shapes of the characters either. This can result in several different kinds of problems, but what you show is pretty typical of one such class of problem. Although you haven't mentioned it, I can also see several places in the document where text overwrites as well, which is another symptom of exactly the same problem. If this is the case then the Ghostscript back channel transcript will have told you that it was unable to find a font and is substituting a named font for the missing one. I can't tell you if Imagemagick stores that anywhere, my guess would be it doesn't. However you can copy the command line from the ImagMagick profile.xml file and then use that to run Ghostscript yourself, and then you will be able to see if that's what is happening. If this is what is happening then you must either; Create your PDF file with the fonts embedded (this is good practice anyway) Supply Ghostscript with a copy of the missing font as a substitute Live with the text as it is
Prestashop TCPDF validation errors
I'm using tcpdf on generating pdf files out of html-snippets out of the prestashop (Version 1.6) source code. Everything seems fine on viewing the pdf in Adobe Reader. On trying to send it via an electronic-fax service (e-post.de) i'm not abled to upload the file,...sadly without an error log or message. On checking generated file on http://www.pdf-tools.com/pdf/validate-pdfa-online.aspx, i'm getting this result: Validating file "AYTKXFQRB_TestEins.pdf" for conformance level pdfa-3b The required XMP property 'pdfaid:part' is missing. The required XMP property 'pdfaid:conformance' is missing. The embedded font program 'AAAAAB+ArialMT' cannot be read. A device-specific color space (DeviceRGB) without an appropriate output intent is used. The embedded font program 'AAAAAC+ArialMT,Bold' cannot be read. The glyph for character 8364 in font 'AAAAAB+ArialMT' is missing. The glyph for character 8364 in font 'AAAAAC+ArialMT,Bold' is missing. The width for character 0 in font 'AAAAAB+ArialMT' does not match. The width for character 0 in font 'AAAAAC+ArialMT,Bold' does not match. The document does not conform to the requested standard. The document contains device-specific color spaces. The document contains fonts without embedded font programs or encoding information (CMAPs). The document's meta data is either missing or inconsistent or corrupt. Done. I think, there are a lot of warnings which can be ignored. Files generated directly out of Adobe InDesign are also containing the majority of these warnings. The only warnings which seems to be hurting are these: The required XMP property 'pdfaid:part' is missing. The required XMP property 'pdfaid:conformance' is missing. Any ideas how to resolve this issue? EDIT: Solution for the xmp-Error: Setting the flag pdfa=true on calling the constructor to activate the pdfa_mode ... public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) But I have to still get rid of the font-errors :-(
How to fix that adding a custom font in tcpdf results in dotted output?
I want to add a custom font. I converted an otf file to ttf, and load them via: $std = \TCPDF_FONTS::addTTFfont($frutigerStd, 'TrueTypeUnicode', '', 96); These command seem to do something as these values are set, as $std will have the value frutigerltstdcn. And use set them, in my extended TCDP class via: $this->SetFont($std); Yet once I open my generated pdf, Adobe Reader will declare: Cannot extract the embedded font 'AAAAAC+FrutigerLTStd-Cn'. Some characters may not display or print correctly. And true enough, the result is a dotted mess: What am I missing or doing wrong?
It turned out that my font was erroneous. It was generated by converting an otf font to ttf, and even though I can use that generated font within MacOS, it has issues with TCDP. Once I got the font as an actual true type file, the issue was resolved.
On the fly pdf creation with 16bit colour depth png support in php
I'm trying to create pdf documents on the fly in an application, i.e. a user clicks a link and a pdf document is displayed to them with some text and some images. I'm currently using FPDF v1.6 (http://www.fpdf.org/) which supports 24bit (true colour) png's but the problem I have is that this is a legacy application and there's 1000's of png's that are of 16bit colour depth which FPDF does not support and I can't simply convert due to other parts of the application using these images. The only solutions I see are: convert the 16bit png image on the fly and embed that into the pdf. find a new class pdf class that will accept 16bit colour depth png's. Anyone have any ideas?
Maybe you could try using TCPDF (never used it with 16bit PNGs but it should be easy to test it).
Fixed with this in python: def fix_16_bit_depth_not_supported(raw_image_path): """ fix RuntimeError: FPDF error: 16-bit depth not supported: test.png """ new_file, filename = tempfile.mkstemp(suffix='.png') os.close(new_file) i = cv2.imread(raw_image_path, cv2.IMREAD_UNCHANGED) img = np.array(i, dtype=np.float32) convert = img / 255. cv2.imwrite(filename, convert) return filename