How to implement custom fonts in TCPDF - pdf

In TCPDF, there are only a few fonts to choose from, to create pdf files. I want to set Tahoma as my pdf font. How can I include Tahoma in TCPDF??

The latest TCPDF version automatically convert fonts into TCPDF format using the addTTFfont() method. For example:
// convert TTF font to TCPDF format and store it on the fonts folder
$fontname = TCPDF_FONTS::addTTFfont('/path-to-font/FreeSerifItalic.ttf', 'TrueTypeUnicode', '', 96);
// use the font
$pdf->SetFont($fontname, '', 14, '', false);
For further information and examples, please check the TCPDF Fonts documentation page.
NOTE: Once the font has been converted, TCPDF no longer requires the TTF file or the above call to addTTFfont()!

I have discovered a very good tool online.
The only thing you need to do is to upload your .ttf file and then download the files and copy then into the /fonts folder.
https://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf

The addTTFfont method is not available on TCPDF main class so following worked for me.
// convert TTF font to TCPDF format and store it on the fonts folder
$fontname = TCPDF_FONTS::addTTFfont('pathto/arial.ttf', 'TrueTypeUnicode', '', 96);
// use the font
$pdf->SetFont($fontname, '', 14, '', false);
Hope this helps!

the below lines will generate 3 files in ur fonts folder
1.rotisserifi56.php
2.rotisserifi56.ctg
3.rotisserifi56.rar
use this to generate the required php and other files
$fontname = $this->pdf->addTTFfont('D:/wamp/www/projectname/sites/all/modules/civicrm/packages/tcpdf/fonts/Rotis Serif Italic 56.ttf', 'TrueTypeUnicode', '', 32);
// use the font
$this->pdf->SetFont($fontname, '', 14, '', false);
Now,
use the fonts like this:
$this->pdf->AddFont('rotisserifi56', '', 'rotisserifi56.php');
$this->pdf->SetFont('rotisserifi56');
--hope this helps some one :)

First create .php ,.afm,.z from http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf and move all three in same directory containing .ttf file.
then Use This :
$pdf->AddFont(path-to/universe.ttf','',path-to/universe.php');
$pdf->SetFont(path-to/universe.ttf','',10);

I don't know anything about tcpdf or php, but I found this:
http://www.tcpdf.org/examples/example_033.phps
Apparently you just use the font's name, not the file name.
Strike one!
Okay, how about this page. To prep a font to be used by TCPDF, you have to run the file through a command line utility and a PHP script.
$ ttf2ufm -a -F myfont.ttf
And then:
$ php -q makefont.php myfont.ttf myfont.ufm
or
MakeFont(string $fontfile, string $fmfile [, boolean $embedded [, $enc="cp1252" [, $patch=array()]]])
ttf2ufm is distributed with TCPDF in the TCPDF/fonts directory.

I found the addTTFfont method a little finicky (ok, probably didn't try very hard) and the online tool mentioned above works for FPDF but misses the ctg.z file that TCPDF requires.
This one - http://fonts.snm-portal.com/ - generates the 3 required files (.php, .z and .ctg.z) for TCPDF. Convert the TTF, upload the three files to your fonts folder and you're good to go.

the best way i have been tried and worked 100%
put your TTF font in fonts folder and then use this constant K_PATH_FONTS + FONT NAME
$font1 = $this->pdf->addTTFfont(K_PATH_FONTS . 'arial.ttf', 'TrueTypeUnicode', '', 8);
$this->pdf->SetFont($font1, '', 15, '', false);

Latest TCPDF supports custom fonts.
Documentation about using custom fonts with TCPDF can be read here.

Create new font by the following script after adding php in your path
php /path_to_tcpdf_directory/tools/tcpdf_addfont.php -i font_name.ttf,font_nameb.ttf,font_namei.ttf
Then use the font by following code
$pdf->SetFont('Font_name');
Name of the font can be found in the /path_to_tcpdf_directory/fonts/font_name.php
To add php into your environment variables, refer How to set the env variable for PHP?

There is no point in using addTTFfont() if you don't have the .ttf file.
And the whole point is: if there is NO ttf file, how can someone use addTTFfont() function?
For example, there is no cid0cs file in font/ directory (TCPDF 6.0.20), only cid0cs.php, which is NOT a font file.

I wasn't able to find addTTFFont() in the latest release of tcpdf.
However, I was able to include a custom TTF file by myself. Within the tcpdf folder is a folder named tools which include a PHP file called tcpdf_addfont.php.
I symlinked it to my home directory (you can omit this step) and run it like this:
shell
./addfont.php -b -t TrueTypeUnicode -f 32 -i myfont.ttf
That's it, it will dump something similar to this:
```
Converting fonts for TCPDF:
*** Output dir set to /Users/pascalraszyk/XXX/XXX/pdf/vendor/tecnick.com/tcpdf/fonts/
+++ OK : /Users/pascalraszyk/XXX/XXX/pdf/myfont.ttf added as myfont
Process successfully completed!
```
Note: There are more examples within the tools folder for various font types.
I used composer to checkout tcpdf.
As long as you put your font definition files in the tcpdf/fonts folder you should be good to go!
I used HTML2PDF which uses tcpdf under the hood and it worked like a charm.

I had this problem despite having all the right files. I had to change the file names to all lower case

When was not possible convert font GothamRounded standard way, I was looking for another tool and only this one http://fonts.snm-portal.com/ worked for me.

Related

Table of contents sidebar in Sphinx LaTeX PDF

I am generating a LaTeX document from Sphinx, and converting it to PDF using pdflatex (from MikTeX). The document is missing a table of contents in the sidebar of the PDF viewer.
If I add manually \usepackage{hyperref} to the tex file, it works. But how can I tell Sphinx to do it in the conf.py project file? There is no (evident) related option in the latex output options.
Thanks!
Section 2.5.3 Customizing the rendering of the Sphinx document mentions:
LaTeX preamble
Additional commands may be added as preamble in the generated LaTeX file. This is easily done by editing file conf.py:
f = open('latex-styling.tex', 'r+');
PREAMBLE = f.read();
latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'a4paper',
# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
'preamble': PREAMBLE
}
This will copy the contents of file latex-styling.tex (in same directory as conf.py) to the generated LaTeX document. For instance, if latex-styling.tex reads:
% My personal "bold" command
\newcommand{\mycommand}[1]{\textbf{#1}}
the generated LaTeX document becomes:
% Generated by Sphinx.
\def\sphinxdocclass{report}
\documentclass[a4paper,10pt,english]{sphinxmanual}
% snip (packages)
% My personal "bold" command
\newcommand{\mycommand}[1]{\textbf{#1}}
\title{My Extension Documentation}
\date{2013-06-30 22:25}
\release{1.0.0}
\author{Xavier Perseguers}
Other options
The configuration file conf.py lets you further tune the rendering with LaTeX. Please consult http://www.sphinx-doc.org/en/stable/config.html#options-for-latex-output for further instructions.
A more direct way of adding content rather than inserting it in a separate file (say, latex-styling.tex), is to specify if verbatim. The next subsection in the documentation mentions this for a specific package typo3:
TYPO3 template
We want to stick as much as possible to default rendering, to avoid having to change the LaTeX code generation from Sphinx. As such, we choose to include a custom package typo3 (file typo3.sty) that will override some settings of package sphinx. To include it automatically, we simply use the preamble option of conf.py:
latex_elements = {
# Additional stuff for the LaTeX preamble.
'preamble': '\\usepackage{typo3}'
}
It's better to contain your styling options in a separate latex-styling.tex file that you can include using the preamble key via an f.read(). That way you don't have to update conf.py. Compartmentalization is usually better.

Inkscape "PDF + Latex" export

I'm using inkscape to produce vector figures, save them in SVG format to export them later as "PDF + Latex" much in the vein of TUG inkscape+pdflatex guide.
Trying to produce a simple figure, however, turns out to be extremely frustating.
The first figure
is an example of the figure I would like to export in the form of "PDF + Latex" (shown here in PNG format).
If I export this to a PDF figure without latex macros the PDF produced looks exactly the same, except for some minor differences with the fonts used to render the text.
When I try to export this using the "PDF + Latex" option the PDF file produced consists on a PDF document of 2 pages (again as .png here):
This, of course, does not looks good when compiling my latex document. So far the guide at TUG has been very helpful, but I still can't produce a working "PDF + Latex" export from inkscape.
What am I doing wrong?
I worked around this by putting all the text in my drawing at the top
select text and then Object -> Raise to top
Inkscape only generates the separate pages if the text is below another object.
I asked this question on the Inkscape online discussion page and got some very helpful guidance from one of the users there.
This is a known bug https://bugs.launchpad.net/ubuntu/+bug/1417470 which was inadvertently introduced in Inkscape 0.91 in an attempt to fix a previous bug https://bugs.launchpad.net/inkscape/+bug/771957.
It seems this bug does two things:
The *.pdf_tex file will have an extra \includegraphics statement which needs to be deleted manually as described in the link to the bug above.
The *.pdf file may be split into multiple pages, regardless of the size of the image. In my case the line objects were split off onto their own page. I worked around this by turning off the text objects (opacity to zero) and then doing a standard PDF export.
If you can execute linux commands, this works:
# Generate the .pdf and .pdf_tex files
inkscape -z -D --file="$SVGFILE" --export-pdf="$PDFFILE" --export-latex
# Fix the number of pages
sed -i 's/\\\\/\n/g' ${PDFFILE}_tex;
MAXPAGE=$(pdfinfo $PDFFILE | grep -oP "(?<=Pages:)\s*[0-9]+" | tr -d " ");
sed -i "/page=$(($MAXPAGE+1))/,\${/page=/d}" ${PDFFILE}_tex;
with:
$SVGFILE: path of the svg
$PDF_FILE: path of the pdf
It is possible to include these commands in a script and execute it automatically when compiling your tex file (so that you don't have to manually export from inkscape each time you modify your svg).
Try it with an illustration that is less wide.
Alternatively, use a wider paperwidth setting.

PDF font - .afm file "bad /BBox" error

I'm working on creating PDF files in PHP with the R&OS PDF class (http://www.ros.co.nz/pdf) trying to add some more fonts to the output. It requires a TTF file and an AFM file for the metrics, but I can't get around the "bad /BBox" error as referred to in this post: Generating .afm from .ttf
At first I thought it was that I wasn't using properly made ttf files, but I've tried converting some fonts that I know are legit (Arial) and I still get the error, so now I think it must be the AFM conversion.
The Stack post above refers to http://fpdf.fruit-lab.de/ as a good conversion tool- maybe I'm just using the wrong mapping? Is there a specific map format the PDF prefers?
I ran into the same problem using dompdf http://code.google.com/p/dompdf/ which uses R&OS PDF Class for rendering so this may help you.
If you have a .ttf font skip to step 2.
Upload your font to http://www.files-conversion.com/font-converter.php (Note: This website does have .afm as an option but the output file was 3mb vs 39kb from fruit-lab) select .ttf and save it.
Upload your .ttf to http://fpdf.fruit-lab.de/ and download the .afm file.
Copy both the .ttf and .afm file to your font directory and make sure they are exactly the same name and case!
I had problems with spaces so I used CamelCase and my .ttf extension was uppercase and my .afm extension was lower case. Once I made sure I had the .afm and .ttf named in CamelCase with no spaces and lower case extensions it worked!
This is probably dompdf specific, I then had to update dompdf_font_family_cache.dist.php and include my font in the array like so:
'testscript' => array (
'normal' => DOMPDF_FONT_DIR . 'TestScript',
'bold' => DOMPDF_FONT_DIR . 'TestScript',
'italic' => DOMPDF_FONT_DIR . 'TestScript',
'bold_italic' => DOMPDF_FONT_DIR . 'TestScript',
)
Note: I only wanted one version of the font - not bold, italic etc so I just made them all point to the same font file.

How can I allow more file extensions with drupal file uploads?

I've got a module that has to let users upload files and everything works as long as the files are in the standard array of allowed extensions. I've tried using file_validate_extensions, but this doesn't seem to change anything.
This is the code I'm using to upload now (the docx extension is added to the standard drupal allowed ones, but it doesn't seem to get picked up):
$fid = $form_state['values']['attachment'];
$file = file_load($fid);
if($file != null){
file_validate_extensions($file, "jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp docx");
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
}
I just looked to this Drupal API, and it seems that you can use the function "file_save_upload" (with $validator as an array of valid extension), this get the file in a temporary state. And then, you have to call "file_save" to make it permanent.

How can I change the margins on a PDF document created by Doxygen?

I am using doxygen to generate a PDF of my code documentation. The PDF has very big margins when using PAPER_TYPE = letter. It looks OK when using a4wide but I would like to have more control over it. I want to use a package called geometry but can't figure out where to add code like this:
\usepackage[top=2.9cm,left=2in,bottom=1in,right=1in]{geometry}
I would like to not have to change the doxygen-generated tex files if possible.
In your Doxyfile, add or edit the EXTRA_PACKAGES line:
EXTRA_PACKAGES = mydoxy
Then create a new file called mydoxy.sty:
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{mydoxy}[2009/12/29 v1.0.0 csmithmaui's Doxygen style]
\RequirePackage[top=2.9cm,left=2in,bottom=1in,right=1in]{geometry}
% any other custom stuff can go here
\endinput
Drop that mydoxy.sty where LaTeX can find it.
The EXTRA_PACKAGES line will tell Doxygen to add \usepackage{mydoxy} to the preamble of the .tex files it generates. This will cause LaTeX to look for a file named mydoxy.sty. In the mydoxy.sty file that we've created, we can add whatever LaTeX code we like (before the \endinput line). Feel free to drop any other customizations you like in this style file.
Note that I haven't tested this, and I'm making a number of assumptions that may be false. But it should at least get you started.