Embed Fonts in TCPDF - tcpdf

I have read through previous threads on this topic, but all are old, unanswered, and/or about something more specific about creating custom fonts.
In TCPDF:
$pdf->SetFont('times', '', 10);
The pdfs I create look awesome. It is just that BarnesAndNoble says my submitted PDF does not contain "embedded fonts". Kindle says the same, but that they then embedded them for me. I'm trying LuLu next, but want to deal with BN.com first.
An example suggests:
$fontname = $pdf->addTTFfont('/fonts/arial.ttf', '', '', 32);
... but does not go on to say what to do with "$fontname". Nor what the "32" specifically stands for, or if there are better options for that "32". Or, if THAT does embed fonts!
My webhost says my fonts path is:
/usr/share/fonts/default/Type1
... maybe then (case sensitive??):
/usr/share/fonts/default/Type1/Helvetica
I need to properly embed my fonts!

TCPDF will just automatically embed fonts you've added on its own. I'm fairly certain it's presumed that most all OS's have Times, Helvetica and Courier already available and therefore TCPDF does not embed them. So in your case, if you add a TTF font TCPDF should recognize when it is used and embed the font definition automatically.
Also, you should only have to do $fontname = $pdf->addTTFfont('/fonts/arial.ttf', '', '', 32); once, so you could run your script once, and then comment that line out.
Direct From the documentation;
AddFont( $family, $style = '', $fontfile = '', $subset = 'default' )
Imports a TrueType, Type1, core, or CID0 font and makes it available. It is necessary to generate a font definition file first (read /fonts/utils/README.TXT). The definition file (and the font file itself when embedding) must be present either in the current directory or in the one indicated by K_PATH_FONTS if the constant is defined. If it could not be found, the error "Could not include font definition file" is generated.
Parameters
$family -
Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
$style - Font style. Possible values are (case insensitive):
empty string: regular (default)
B: bold
I: italic
BI or IB: bold italic
$fontfile - The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
$subset - if true embedd only a subset of the font (stores only the information related to the used characters); if false embedd full font; if 'default' uses the default value set using setFontSubsetting(). This option is valid only for TrueTypeUnicode fonts. If you want to enable users to change the document, set this parameter to false. If you subset the font, the person who receives your PDF would need to have your same font in order to make changes to your PDF. The file size of the PDF would also be smaller because you are embedding only part of a font.
Returns - array containing the font data, or false in case of error.
Since
1.5
See
TCPDF::SetFont(), TCPDF::setFontSubsetting()
Public

Related

How use UTF-8 fonts with gem CombinePDF?

I use gem combine_pdf for load existed pdf file and add some text (in French) to it.
But some specific charactets displaying poorly:
https://drive.google.com/file/d/1LAiK3JJTDjbVpgXrA9ITfCTeYSVws37x/view?usp=sharing
I try:
CombinePDF::Fonts.register_font(Rails.root.join('app/assets/fonts/slick.ttf'), font_metrics, Type: :Font)
font_metrics i take from Helvetica font from initiate_library function (CombinePDF::Fonts).
But it not work... What am I doing wrong?
I had the exact same problem with the combine_pdf gem. I did manage to fix it following the recommendations in the combine_pdf #137 issue.
I created a PDF file with all the characters needed with a font that includes those characters (basically the entire alphabet, including the special characters é à ê as well as € or ' characters).
Register the font as an available font in combine_pdf (e.g. your controller):
fonts = CombinePDF.new(Rails.root.join('lib', 'data','font_template.pdf').to_s).fonts(true)
CombinePDF.register_font_from_pdf_object :new_font, fonts[0]
Set the font setting in your texbox to the new font with font: :new_font (or set it globally in an initialiser).
You can see a full example here in the combine_pdf_demo
The biggest problem for me was to correctly embed the new font in the PDF template. You need to make sure the fonts are actually embedded in the PDF, as sometimes system-fonts don't get properly embedded. Also make sure to include all the characters needed in your text.

Custom Fonts only available when set in Interface Builder

I have added a custom font to my project.
It is included in the target, and it is added in the plist.
When I try to use it programmatically it doesn't work, and it doesn't show up when I print out a list of available fonts.
However, it does show up as an option in Interface Builder, and if I set a label's text to that font in IB, it works correctly and shows up when I print out a list of available fonts.
This is XCode 6.4 and iOS 8.0
When it is working via IB, it gets printed out in the font names like this:
Special Elite
SpecialElite-Regular
I call the font programmatically like:
[UIFont fontWithName:#"SpecialElite-Regular" size:fontSize];
There are no problems when doing this with the built-in fonts.
When I try to use it programmatically it doesn't work, and it doesn't show up when I print out a list of available fonts
This proves that you have not in fact included it properly in the target and the Info.plist.
The reason it seems to work in IB is that this font is also present on your computer. But in fact if you were to run this app on your device, you would see that even setting the font in IB is not working.
Your font is Special Elite. As you can see, I have it visible here in my running app:
Here's the code that I used:
let lab = UILabel()
lab.text = "This is a test"
lab.font = UIFont(name:"SpecialElite-Regular", size:18)
lab.sizeToFit()
lab.frame.origin = CGPointMake(100,100)
self.view.addSubview(lab)
So you see, it is possible to refer to this font in code — if it is loaded properly. You are evidently not loading it properly. It's not in your app bundle, or it's not in the copy build phase, or it's not correctly listed in your Info.plist.
(Of course there's always a possibility that you're calling [UIFont fontWithName:size:] with a bad value for the name or for the size.)
Ok, topic is old but I would like to put in my two cents here.
First off all, read this article http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
In brief:
remember to include your fonts to project
setup target for fonts
check if your fonts are included as resources in bundle
add custom fonts to plist file
check the real name of your font in code
When I did everything like described in this article, my fonts works in interface builder only. I also have problem with step 5 - my fonts did not show up in console output.
Ok, so now is time for tips from me:
TIP #1
use otf font format, if you have different, convert it (or ask your designer ;) )
you can use this site for that: https://onlinefontconverter.com/
TIP #2
In article above you can find:
Open it and add a new row called “Fonts provided by application” which
will be an array that you need to add all the filenames of the fonts
you want to use.
Instead of "Fonts provided by application" enter "UIAppFonts"
Remember, if you have more than one plist file, set this for all.
As mentioned by others, following article is very useful to diagnose the problem.
http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
Also while adding custom fonts to Xcode project, add individual font and not the directory in which all fonts are stored. If you add that directory, fonts will be visible in Interface Builder but not in the running app.
Matt did a great job of isolating the problem... I was not loading the font correctly.
In my case the problem was in the Info.plist.
I was listing the font in the Info.plist using its name. This is incorrect. You are supposed to list the font in the Info.plist using its file name.
I know this is old but I just ran into the same issue. The font would show up only if I selected that font in the interface builder. Turns out when I searched for UIAppFonts, it didn't list the main Info.plist (we were already using custom fonts) for some reason. So I ended up adding the fonts to the localized (?) plist file which resulted in this issue. What I had to do was to go to the Project Settings > Target > Info tab and add the font file names there.
Issue is You are not giving ProperName Of Fonts in Code, for Getting Name of Fonts please run this code.
for family: String in UIFont.familyNames
{
print("\(family)")
for names: String in UIFont.fontNames(forFamilyName: family)
{
print("== \(names)")
}
}
it will prints all fonts, search your font and give proper name.
let attributedText = NSMutableAttributedString(string: "We
Apologize! \n This feature is only available for \n Go 4.0
Subscriptions", attributes: [NSAttributedStringKey.font :
UIFont(name: "SpecialElite-Regular", size: 22)!])

Change the encoding of a file in Visual Studio Code

Is there any way to change the encoding of a file?
For example UTF-8 to ISO 8859-1?
Setting Example Sublime Text:
"default_encoding": "UTF-8"
So here's how to do that:
In the bottom bar of VSCode, you'll see the label UTF-8. Click it. A
popup opens. Click Save with encoding. You can now pick a new
encoding for that file.
Alternatively, you can change the setting globally in Workspace/User settings using the setting "files.encoding": "utf8". If using the graphical settings page in VSCode, simply search for encoding. Do note however that this only applies to newly created files.
Apart from the settings explained in the answer by #DarkNeuron:
"files.encoding": "any encoding"
you can also specify settings for a specific language like so:
"[language id]": {
"files.encoding": "any encoding"
}
For example, I use this when I need to edit PowerShell files previously created with ISE (which are created in ANSI format):
"[powershell]": {
"files.encoding": "windows1252"
}
You can get a list of identifiers of well-known languages here.
The existing answers show a possible solution for single files or file types. However, you can define the charset standard in VS Code by following this path:
File > Preferences > Settings > Encoding > Choose your option
This will define a character set as default.
Besides that, you can always change the encoding in the lower right corner of the editor (blue symbol line) for the current project.

iOS ttf fonts only partially work

I need OpenSans in my app and so I imported the whole OpenSans bunch which includes Bold, ExtraBold, Italic, Regular, Light and more. I added them to Fontbook (to check the exact name I need to address them by when using them) on my Mac, to my project in the file structure and added them in Build phases to my project as well.
Now the weird thing; when I use them with UIFont, ONLY OpenSans-Bold works. The rest doesn't work. If I use OpenSans-Bold (exactly the identifier given by Fontbook), everything works fine. If I, however, change it to OpenSans-Regular or OpenSans-Light or something else in that family, I get the systemfont with UIFont and I get nothing when drawing with Quartz.
All fonts are in all lists and I checked all the identifiers from Fontbook (which was why I couldn't get OpenSans-Bold working), but now i'm at a loss. Any idea what I could be doing wrong?
OpenSans Regular is just OpenSans, semi-bold is OpenSans-Semibold, etc.
In Font Book.app select the font and choose Command + I which brings up the font info. In the "PostScript name" in the top you can see what the font is called and how you can call it in your code.
Also, just make sure the font has actually been added in your bundle and your plist :)
You Can check the fonts provided by the ttf file With the following lines of code..
this method is provided by the UIFont class.
(NSArray *)fontNamesForFamilyName:(NSString *)familyName
You need to add the files in the right locations, then in the plist file, then enumerate them all and print them to get the PDF names so you can access them. Most likely you're using the wrong names.
Fonts have three kinds of names:
The file name
The font name in Font Book
The PDF name - this is the one you use to access the font in code
See this answer:
Adding custom fonts to iOS app finding their real names

LaTeX: Specifying default document-wide font

For some fonts, it's easy to replace the default font with something like: \renewcommand{\rmdefault}{somefont}.
However, some fonts, like BrushScriptX
want you to use a special command like \bsifamily to use the font. In the
\bsifamily is defined in /usr/share/texmf-texlive/tex/latex/brushscr/pbsi.sty:
\DeclareRobustCommand\bsifamily{\fontencoding{T1}\fontfamily{pbsi}%
\fontseries{xl}\fontshape{n}\selectfont}
In this case, I have a document that uses a cls stylesheet that I am editing. I'm using the standard includes to use this font:
\usepackage[T1]{fontenc}
\usepackage{pbsi}
If I try to use \renewcommand{\rmdefault}{bpsi} in my document's template, it fails with the error: Font shape `T1/pbsi/m/n' undefined
What might be the best way to replace the default roman font with BrushScriptX in a template, like \renewcommand{\rmdefault}{otherfont} would?
If you look inside pbsi.sty you'll find
\DeclareRobustCommand\bsifamily{\fontencoding{T1}\fontfamily{pbsi}%
\fontseries{xl}\fontshape{n}\selectfont}
Therefore, to set this font as the default — ugh :) — you'll need to write
\renewcommand\rmdefault{pbsi}
\renewcommand\mddefault{xl}
(\mddefault is the default "non-bold" series used in running text.)

Resources