How to set relative path to font in apache-fop fop.xconf? - java

I have the following fop.conf, which works fine under Windows:
<?xml version="1.0" encoding="utf-8" ?>
<fop>
<renderers>
<renderer mime="application/pdf">
<fonts>
<fonts><font kerning="yes" embed-url="/Windows/Fonts/arial.ttf" embedding-mode="subset">
<font-triplet name="arial" style="normal" weight="400"/></font>
</fonts>
</renderer>
</renderers>
</fop>
But because the Application also needs to run on OS's which do not have Arial provided, I decided to add a the font to src/main/resources/fonts/Arial.ttf in the Java application.
So I'm trying to reference the font like this:
<?xml version="1.0" encoding="utf-8" ?>
<fop>
<renderers>
<renderer mime="application/pdf">
<fonts>
<directory>file:src/main/resources/fonts/</directory>
</fonts>
</renderer>
</renderers>
</fop>
But it does not use the font.
While debugging I saw the fopFactoryBuilder has a field called baseUri, is it possible to add this to the fop.conf and extend it to get the path to the Arial font?
I don't have the fopFactoryBuilder.setFontBaseURL() because I need to use an older version which does not provide it.
What is the best way to provide the relative path inside the fop.conf?

This did the trick:
<?xml version="1.0" encoding="utf-8" ?>
<fop>
<base>.</base>
<renderers>
<renderer mime="application/pdf">
<fonts>
<font kerning="yes" embed-url="app/src/main/resources/fonts/Arial.ttf" embedding-mode="subset">
<font-triplet name="Arial" style="normal" weight="400"/></font>
<autodetect/>
</fonts>
</renderer>
</renderers>
</fop>

Related

apache Fop separated arabic charcters

I am trying to generate PDF from xml using apache Fop,
It works fine with english but while i try to disply arabic text the characters are separated as shown in the Image
1- I tried with 2.1, 2.5 and latest stable apache Fop version 2.6
2- I embded the font in Fop config file
3- I enabled complex-scripts
4- I tried with different fonts
but it didn't solve the issue
I attached Apache Fop configuration file below
<fop version="1.0">
<complex-scripts disabled="false" />
<renderers lang="ar">
<renderer mime="application/pdf">
<fonts>
<font kerning="yes" embed-url="src/main/resources/fonts/trado.ttf" embedding-mode="auto" encoding="UTF-8">
<font-triplet name="trado-ar-font" style="normal" weight="normal" />
</font>
</fonts>
</renderer>
</renderers>
</fop>
I also checked:
XSL-FO: displaying Arabic characters and
FOP Arabic letter spacing
but there is nothing worked with me
appriciate any support please
refrence to https://xmlgraphics.apache.org/fop/2.0/complexscripts.html
It was solved by adding script="arab" to the root tag like:
<fo:root script="arab">

Indic font support in Apache FOP

While using Apache FOP 2.2 from my Java application to print Hindi strings in PDF by using mangal.ttf, some Hindi characters are not displayed correctly.
I am Using JDK 1.8 and spring MVC.
I tried lohit.ttf, devanagari.ttf, aparajita.ttf and kokila.ttf but all have the same issue.
case 1:
When fop-conf.xml is set as below:
<font kerning="yes" embed-url="classpath:/mangal.ttf" >
<font-triplet name="Mangal" style="normal" weight="normal"></font-triplet>
</font>
Result: से is shown like this स े, as shown in this screenshot
case 2:
When fop-conf.xml is set as below:
<font kerning="yes" metrics-url="classpath:/mangal.xml" embed-url="classpath:/mangal.ttf" >
<font-triplet name="Mangal" style="normal" weight="normal"></font-triplet>
</font>
Result: problem listed in case 1 is resolved but I am facing another issue attached as shown in the following screenshot
You can see the expected output at the link https://www.fonts.com/font/microsoft-corporation/aparajita) using this sample text:
से and ग्रामीण should be printed in pdf
Other things I tried:
I tried PDFOne to generate the PDF. Yet the same issue. Windows however seems to show it correctly.
configuring complex-script: <fop version="1.0"> <complex-scripts disabled="true"/> ... </fop>
using the script attribute: <fo:block font-family="ARIALUNI" script="dev2" > देवी ग्रामीण</fo:block>
Is there any configuration setting in FOP that I am missing?
Shorter, general answer:
If the font is configured but the output is not correct, the problem could be FOP incorrectly determining which script mode to use.
Solution: explicitly set the script property in the FO file, using either a standard or an extended script code.
Note that Indic scripts have both a standard code and an extended one (for example deva and dev2 for Devanagari) and the resulting output is different, so you may need to try them both and choose the appropriate one.
Longer answer:
I don't have the fonts you mention available, so I tested using the Amiko Google Open Font.
More importantly, I know nothing about devanagari script, so I really cannot say whether the output is right or wrong, I can only compare it with your images.
This is the complete input file I used, with the sentence copied from your question:
<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="simpleA4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm">
<fo:region-body/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simpleA4">
<fo:flow flow-name="xsl-region-body">
<fo:block font-family="Amiko" script="deva">deva: से and ग्रामीण should be printed in pdf</fo:block>
<fo:block font-family="Amiko" script="dev2">dev2: से and ग्रामीण should be printed in pdf</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
Here is a minimal configuration:
<?xml version="1.0"?>
<fop version="1.0">
<renderers>
<renderer mime="application/pdf">
<fonts>
<font kerning="yes" embed-url="Amiko/Amiko-Regular.ttf">
<font-triplet name="Amiko" style="normal" weight="normal"/>
</font>
</fonts>
</renderer>
</renderers>
</fop>
which produces this output:
If I understand correctly, the output you are trying to achieve is the one with script="deva".
Note that the metrics-url attribute in your configuration is not needed.
Moreover, putting <complex-scripts disabled="true"/> in your configuration has the effect of disabling the "complex script" support, so I expect this to produce the wrong output.
This configuration
<?xml version="1.0"?>
<fop version="1.0">
<complex-scripts disabled="true"/>
<renderers>
<renderer mime="application/pdf">
<fonts>
<font kerning="yes" embed-url="Amiko/Amiko-Regular.ttf">
<font-triplet name="Amiko" style="normal" weight="normal"/>
</font>
</fonts>
</renderer>
</renderers>
</fop>
produces the following output:
This might help others who are struggling to get Indic language support in Apache FOP. I tried to render Hindi with Amiko font and it works like a charm.
I wanted to see if other Indian languages are supported in the same way, after a bit of struggle I could see Kannada can also be rendered .
<font kerning="yes" embed-url="Benne-Regular.ttf">
<font-triplet name="Benne" style="normal" weight="normal"/>
</font>
But its mandatory to mention the script in the XSL .
<fo:block font-family="Benne" script="knda" font-size="16pt"> ಕನ್ನಡ
ನನಗೆ ಇನ್ನೂ ಒಂದು ಪ್ರಶ್ನೆ ಇದೆ.</fo:block>

Configure Batik to utilize custom fonts

I have an issue configuring Batik PDFTranscoder for Svg to Pdf conversion.
I want to embed custom truetype fonts to PDF output, therefore I utilize Batik transcoder. I supply font configuration in fop config file, as described here: https://xmlgraphics.apache.org/fop/2.2/fonts.html
I tried conversion with org.apache.xmlgraphics.Fop versions 2.1 and 2.2. Without any success.
My pdf output, comes with 'Times New Roman' font instead of coming with embedded font 'Arial', as it should, based on example below:
My config file:
<?xml version="1.0" encoding="UTF-8"?>
<fop version="1.0">
<renderers>
<renderer mime="application/pdf">
<fonts>
<directory>C:/path/to/fontsfolder</directory>
<auto-detect/>
</fonts>
</renderer>
</renderers>
</fop>
Here's my transcoder code:
import org.apache.batik.transcoder.*;
import org.apache.fop.svg.PDFTranscoder;
import org.w3c.dom.Document;
PDFTranscoder transcoder = new PDFTranscoder();
try {
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg = cfgBuilder.buildFromFile(new File("path-to-xml-config-file"));
ContainerUtil.configure(transcoder, cfg);
} catch (Exception e) {
throw new TranscoderException(e);
}
transcoder.transcode(new TranscoderInput(doc),
new TranscoderOutput(os));
Sample svg file:
<defs>
<style type="text/css">
#font-face{font-family:'Arial';font-style: normal;font-weight:
400;src:url('C:/path/to/fontsfolder/arial.ttf')
format('truetype');}
</style>
</defs>
<text x="55" y="245" style="font-family: 'Arial'; font-weight:normal;
font-style: normal" >Sample text</text>
Can anyone tell me what I am missing here with Transcoder configuration? Embedding svg fonts into svg file is not an option in this case.
Update the config file and move fonts section outside the renderers section, like this:
My config file:
<?xml version="1.0" encoding="UTF-8"?>
<fop version="1.0">
<fonts>
<directory>C:/path/to/fontsfolder</directory>
<auto-detect/>
</fonts>
</fop>
Bulk font configuration from Apache website is simply wrong:
https://xmlgraphics.apache.org/fop/2.2/fonts.html
This answer was posted as an edit to the question Configure Batik to utilize custom fonts by the OP odoko under CC BY-SA 3.0.

Issue with Apache FOP 1.1 <auto-detect/> on CentOS

I have an issue with Cyrillic font in Apache FOP 1.1 on CentOS: ######## is shown instead of symbols.
This is fragment from fop.xconf:
<fonts>
<font-triplet name="Arial" style="normal" weight="bold"/>
<auto-detect/>
</fonts>
On Windows 10 it works fine, on CentOS it produces ######.
Can anybody help me?
The font-triplet element should be inside a font element pointing to the font file, for example:
<font kerning="yes" embed-url="/usr/share/fonts/Arial.ttf" embedding-mode="subset">
<font-triplet name="Arial" style="normal" weight="bold"/>
</font>
Moreover, it seems like FOP cannot find a font folder in the "usual" positions; you can try explicitly adding a font folder
<directory>/usr/share/fonts/</directory>
The specific error message(s) you get could help pinpointing the specific problem.

Which swing tool is used?

I have a java program installed on my computer that creates Forms and can later print it. I want to do something similar with my program! In the source folders of this program, I found folders for the specific forms. Each folder containes a image for e.g named Payment.png and a xml file is structured like this:
<?xml version="1.0" encoding="windows-1250" standalone="no"?>
<form xmlns:xinclude="http://www.w3.org/2001/XInclude" blankodruck="true" filename="Payment.png" saveAndPrintButtonEnabled="true" scale="1.0" title="Payment" window_height="725" window_width="860" paper="A5" orientation="landscape">
<printscale sx="0.432" sy="0.434"/>
<printoffset x="0" y="13"/>
<model class="de.hans.client.app.form.payment.PaymentModel" />
<printform filename="Payment.png" pngscaleX="1" pngscaleY="1"/>
<offset x="-20" y="15" scaleX="0.72" scaleY="0.7" printOffsetX="4" printOffsetY="3" printScaleX="2.34" printScaleY="2.33">
<xinclude:include href="../Header.xml"/>
</offset>
<checkbox height="33" name="Paydaten_ChB" width="35" x="862" y="74" printOffsetX="0" printOffsetY="0"/>
<checkbox columns="90" height="35" ...
<textfield columns="1" height="36" name="quartal" width="35" x="1235" y="72" printOffsetX="0" printOffsetY="0"/>
<radiobutton group="Gender" height="36" name="User_gender" tooltip="men" width="33" x="1253" y="130" printOffsetX="0" printOffsetY="0"/>
It has even a barcode-field:
<barcode length="886" type="3" px="430" py="180" pages="1">
<invisible name="date"/>
<invisible name="titel"/>
Can somebody guess how this printable swings are created in the program? Thanks

Categories

Resources