Generate multi lingual PDF with Flying Saurce? - flying-saucer

I generate pdf from xhtml file but font style is differs with XMHTL.
Here is Java Code
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont("C:/Windows/Fonts/times.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.setDocument(urlXhtmlFile);
renderer.layout();
renderer.createPDF(os);
os.close();
How to set font style of pdf and html is same (color). Thanks!!!

You should add CSS into your XHTML, there you can manipulate with font size, color etc (change font properties according to your needs):
<html>
<head>
<style type="text/css" media="print">
body {
font-family: "Times New Roman", Times;
font-size: 10pt;
color: blue;
}
</style>
</head>
<body>
</body>
</html>
Also, I suggest you embed font into PDF, as someone opening this PDF without this font installed would not see PDF properly.

Related

Is it possible to use multiple fonts at the same time?

I want to use 2 fonts at the same time for English and Arabic characters. Haven't been able to figure out how to do it :(
These are the fonts I'm trying to use.
Arabic: https://fonts.google.com/specimen/Almarai?query=almarai
English: https://fonts.google.com/specimen/Raleway?query=raleway
On web, it would be as simple as font-family: Raleway, Almarai, sans-serif, Arial;
If anyone knows how to do it, I would really appreciate the help!!
#import url('https://fonts.googleapis.com/css2?family=Almarai&display=swap');
#import url('https://fonts.googleapis.com/css2?family=Almarai&family=Raleway&display=swap');
body {
font-family: 'Almarai', sans-serif;
}
h2 {
font-family: 'Raleway', sans-serif;
}
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World! is in this font style and I am too!</h1>
<br>
<h2>I am styled as RALEWAY</h2>
</body>
</html>
Explanation: Google fonts provide rules to specify in CSS to import it as you do it also specifies how you use the font in this example I've demonstrated how. you use the import("font link here") to import the fonts or you could link them in the head. then you use the font-family: 'font name here' with an element {} to specify that the font is active. Then that element should be in that font.

Library html-pdf gives font as italic style

I use html-pdf to convert my html to pdf. But when I include my font as
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght#400;500;600;700;800&display=swap" rel="stylesheet">
And write in css:
html {
font-family: 'Poppins', sans-serif;
font-weight: 400;
font-size: 14px;
}
It renders my font as italic. But in HTML it is normal font. How to solve it? I tried font-face, it did not work

cfhtmltopdf not working with googleapis fonts

Why the cfhtmltopdf does not display the googleapis font?
<cfhtmltopdf encryption = "AES_128">
<html>
<head>
<!--Add GoogleFonts-->
<link href="https://fonts.googleapis.com/css?family=Aguafina+Script|Alex+Brush|Bilbo|Condiment|Great+Vibes|Herr+Von+Muellerhoff|Kristi|Meddon|Monsieur+La+Doulaise|Norican|Nothing+You+Could+Do|Parisienne|Permanent+Marker|Sacramento|Yellowtail" rel="stylesheet">
</head>
<body>
<p id="displaySignature" style="font-family: Meddon, cursive; font-size: 30px;">Test</p>
</body>
</html>
</cfhtmltopdf>
It ignores the font style.
You can goto the ColdFusion administrator -> Server Settings -> Font Management and import the fonts that are required by you and try.
If you look at this answer, using
#import url('https://fonts.googleapis.com/css?family=Open+Sans');
will import the contents of the CSS file into your HTML. Using #import will actually "defer the loading of the included resource until the file is fetched", which may be exactly what you need.

how to create separated pages in same pdf document using play pdf

I'm using play2-pdf to generate pdf in my application I created firstly the cover page but when I try to add the other pages its added in the same pdf page how can I create multiple pages in the same pdf document also I'm trying to add a footer to the my first page but still have the same problem
<html>
<head>
<style>
p.serif {
font-family: "Times New Roman", Times, serif;
}
p.sansserif {
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
this is a test
<div style="position:absolute; bottom:0;right:0;margin:24px;">by bSuccess</div>
</body>
</html>
For headers and footers, you can use css paged media module (https://www.w3.org/TR/css3-page/)
Example:
<html>
<head>
<style>
##page {
##bottom-right {
content: "by bSuccess";
font-size: 14px;
}
}
</style>
</head>
<body>
this is a test
</body>
</html>
For page breaks, you can use css page break properties (https://css-tricks.com/almanac/properties/p/page-break/)
Example:
<html>
<head>
<style>
##page {
##bottom-right {
content: "by bSuccess";
font-size: 14px;
}
}
</style>
</head>
<body>
<div>this is a test</div>
<p style="page-break-after:always;"></p>
<div>testing page break</div>
</body>
</html>

iTextSharp HTML to PDF using Font Awesome

I am already successfully converting HTML to PDF using iTextSharp/XMLWorkerHelper. So far, I have been able to apply internal styling to the HTML as shown below. This works just fine. However, when I try to use a CDN to utilize Font Awesome, I am having no such luck.
This is what my HTML looks like:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<style>
body {
color: #000000;
font-size: 14px;
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<i class="fa fa-briefcase"></i>
</body>
</html>
And this is how I'm converting the HTML to a memory stream which is ultimately used to create the PDF via FileStreamResult (the FileStreamResult is closing the memory stream).
string html = "<html here>";
var ms = new MemoryStream();
using (var doc = new Document())
{
using (var writer = PdfWriter.GetInstance(doc, ms))
{
doc.Open();
using (var srHtml = new StringReader(html))
{
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
}
writer.CloseStream = false;
doc.Close();
}
}
Why am I not able to see the briefcase on my generated PDF?