OpenPDF icon indicating copy to clipboard operation
OpenPDF copied to clipboard

Bengali font is always broken in pdf

Open nazmul-prince opened this issue 3 years ago • 9 comments

Describe the bug I've tried almost all available Bengali font but got broken Bengali words in the pdf. For example I wanted to print the following Bengali line in pdf: আমার নাম নাজমুল আপননিই জেকোনো নামো টোলা বিশ্বাসপাড়া which is broken in the generated pdf as broken (pdf attached)

To Reproduce Code to reproduce the issue

package com.lowagie.examples.directcontent.text;

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;

/**
 * Adding Bengali text in pdf.
 */
public class TextBangla {
	/**
	 * 
	 * @param args no arguments needed
	 */
	public static void main(String[] args) {

		System.out.println("Text at absolute positions");
		
		String fonts[] = {
				"SolaimanLipi_22-02-2012.ttf",
		          "LiAlinurBihongoUnicode.ttf",
		          "SutonnyMJRegular.ttf",
		            "SUTOM.TTF",
		            "SutonnyOMJ.ttf",
		             "AmarBangla.ttf",
		           "Kalpurush.ttf",
		          "Nikosh.ttf",
		          "Rajon_Shoily.ttf",
		          "Kalpurush.ttf",
		          "FreeSans.ttf",
		          "vrinda.ttf",
		          "akshar.ttf"
				};

		for(String banglaFontFileName : fonts)
		try {
			// step 1: creation of a document-object
			Document document = new Document();
			PdfWriter writer = PdfWriter.getInstance(document,
					new FileOutputStream("textbangla_ " + banglaFontFileName + ".pdf"));
            // step 3: we open the document
            document.open();
			document.add(new Paragraph("Adding bangla font: " + banglaFontFileName));

			BaseFont bangla = BaseFont.createFont(
					"/home/nazmul/Downloads/" + "AlinurBihongo/Unicode/" + banglaFontFileName,
					BaseFont.IDENTITY_H,
					BaseFont.EMBEDDED);

			Font font = new Font(bangla, 11f);
			String to_print = "আমার নাম নাজমুল আপননিই জেকোনো নামো টোলা বিশ্বাসপাড়া";
			Paragraph p = new Paragraph(new Phrase(to_print, font));

			document.add(p);
			// step 5: we close the document
			document.close();
		} catch (DocumentException | IOException de) {
			System.err.println(de.getMessage());
		}
	}
}

  1. You just need to run the above code. :)

Expected behavior A clear and concise description of what you expected to happen.

Screenshots

textbangla_-Kalpurush-ttf-pdf

System (please complete the following information):

  • OS: Ubuntu 16.04
  • Used Font: Attached zip file of all Bengali fonts I've used so far:

Bengali_fonts.zip

Here is my resulted PDF files: Resulted_pdf_files.zip

I think its not a bug as I labeled it as bug :). Any help would be appreciated. Thanks in advance.

nazmul-prince avatar Jul 09 '21 12:07 nazmul-prince

Do you have apache fop on classpath?

codecracker2014 avatar Jul 09 '21 12:07 codecracker2014

@codecracker2014 nope I dont. Do I need to add that in my dependencies?

nazmul-prince avatar Jul 09 '21 12:07 nazmul-prince

Yes

codecracker2014 avatar Jul 09 '21 12:07 codecracker2014

@codecracker2014 thanks for your quick comments. I've added that dependencies but still no luck.

Here is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.pdf</groupId>
	<artifactId>bangla</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<name>bangla</name>
	<!-- FIXME change it to the project's website -->
	<url>http://www.example.com</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>

	<dependencies>

		<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/fop -->
		<dependency>
			<groupId>org.apache.xmlgraphics</groupId>
			<artifactId>fop</artifactId>
			<version>2.6</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.github.librepdf/openpdf -->
		<dependency>
			<groupId>com.github.librepdf</groupId>
			<artifactId>openpdf</artifactId>
			<version>1.3.26</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<pluginManagement><!-- lock down plugins versions to avoid using Maven 
				defaults (may be moved to parent pom) -->
			<plugins>
				<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
				<plugin>
					<artifactId>maven-clean-plugin</artifactId>
					<version>3.1.0</version>
				</plugin>
				<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
				<plugin>
					<artifactId>maven-resources-plugin</artifactId>
					<version>3.0.2</version>
				</plugin>
				<plugin>
					<artifactId>maven-compiler-plugin</artifactId>
					<version>3.8.0</version>
				</plugin>
				<plugin>
					<artifactId>maven-surefire-plugin</artifactId>
					<version>2.22.1</version>
				</plugin>
				<plugin>
					<artifactId>maven-jar-plugin</artifactId>
					<version>3.0.2</version>
				</plugin>
				<plugin>
					<artifactId>maven-install-plugin</artifactId>
					<version>2.5.2</version>
				</plugin>
				<plugin>
					<artifactId>maven-deploy-plugin</artifactId>
					<version>2.8.2</version>
				</plugin>
				<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
				<plugin>
					<artifactId>maven-site-plugin</artifactId>
					<version>3.7.1</version>
				</plugin>
				<plugin>
					<artifactId>maven-project-info-reports-plugin</artifactId>
					<version>3.0.0</version>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

nazmul-prince avatar Jul 09 '21 12:07 nazmul-prince

Please add dependency of xmlgraphics-commons as well. Refer this wiki https://github.com/LibrePDF/OpenPDF/wiki/Multi-byte-character-language-support-with-TTF-fonts

codecracker2014 avatar Jul 09 '21 14:07 codecracker2014

@codecracker2014 still having the same problem :(. My current pom is:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.pdf</groupId>
	<artifactId>bangla</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<name>bangla</name>
	<!-- FIXME change it to the project's website -->
	<url>http://www.example.com</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>

	<dependencies>
		<!-- https://mvnrepository.com/artifact/com.github.librepdf/openpdf -->
		<dependency>
			<groupId>com.github.librepdf</groupId>
			<artifactId>openpdf</artifactId>
			<version>1.3.26</version>
		</dependency>
		<dependency>
               <groupId>org.apache.xmlgraphics</groupId>
               <artifactId>fop</artifactId>
                <version>2.3</version>
            </dependency>
            <dependency>
                <groupId>org.apache.xmlgraphics</groupId>
                <artifactId>xmlgraphics-commons</artifactId>
                <version>2.3</version>
           </dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<pluginManagement><!-- lock down plugins versions to avoid using Maven 
				defaults (may be moved to parent pom) -->
			<plugins>
				<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
				<plugin>
					<artifactId>maven-clean-plugin</artifactId>
					<version>3.1.0</version>
				</plugin>
				<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
				<plugin>
					<artifactId>maven-resources-plugin</artifactId>
					<version>3.0.2</version>
				</plugin>
				<plugin>
					<artifactId>maven-compiler-plugin</artifactId>
					<version>3.8.0</version>
				</plugin>
				<plugin>
					<artifactId>maven-surefire-plugin</artifactId>
					<version>2.22.1</version>
				</plugin>
				<plugin>
					<artifactId>maven-jar-plugin</artifactId>
					<version>3.0.2</version>
				</plugin>
				<plugin>
					<artifactId>maven-install-plugin</artifactId>
					<version>2.5.2</version>
				</plugin>
				<plugin>
					<artifactId>maven-deploy-plugin</artifactId>
					<version>2.8.2</version>
				</plugin>
				<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
				<plugin>
					<artifactId>maven-site-plugin</artifactId>
					<version>3.7.1</version>
				</plugin>
				<plugin>
					<artifactId>maven-project-info-reports-plugin</artifactId>
					<version>3.0.0</version>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

nazmul-prince avatar Jul 09 '21 16:07 nazmul-prince

This issue is making me crazy :). Anyone faced this problem ever? Btw special thanks to @codecracker2014 as he tries his best :+1:

nazmul-prince avatar Jul 09 '21 17:07 nazmul-prince

@nazmul-prince you could try to use LayoutProcessor, see https://github.com/LibrePDF/OpenPDF/wiki/Accents,-DIN-91379,-non-Latin-scripts

vk-github18 avatar Oct 04 '21 06:10 vk-github18