Skip to content
Open

Ayush #209

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
164a67d
[fonts and examples added for devanagari]
dura-amar Oct 21, 2024
ba76e06
Submitting to Harish
dura-amar Nov 10, 2024
5f3e19e
[Comments] added for GsubWorkerForDevanagari.java
dura-amar Nov 28, 2024
dab5003
Updated the output generation code.
dura-amar Dec 6, 2024
006a183
Update from working branch
dura-amar Dec 8, 2024
e4f9df7
update
dura-amar Dec 8, 2024
69ec2fa
र्थ्यो: reph repositioning in case of multiple half character solved
dura-amar Dec 8, 2024
24997d7
[Comment] on the half character problem, need feature specific implem…
dura-amar Dec 9, 2024
b83a2ef
Identified the issue with the half characters
dura-amar Dec 10, 2024
9795eea
[UPDATE] solved for सङ्क्à¤षिप्त where the ि ikar fol
dura-amar Dec 10, 2024
8a0288f
[UPDATE] the problem with ending half characters as in छनà¥् à¤for छन…
dura-amar Dec 10, 2024
5f68667
Updated on issue with ि
dura-amar Dec 13, 2024
16527ff
Updated on issue with ि
dura-amar Dec 19, 2024
01c64c0
fix for à¤ि
dura-amar Dec 19, 2024
f050041
added test for र्थ्यो
HarishJoshi001 Dec 21, 2024
a4fcaaf
created GsubWorkerForDevanagariNepali and its test
HarishJoshi001 Jan 21, 2025
726dc77
Updated Kalimati and Kanjirowa fonts
dura-amar Jan 23, 2025
348897c
Added feature wise examples for pdf generation
dura-amar Jan 23, 2025
80c222e
Comment to handle the problem with rakaar:
dura-amar Jan 24, 2025
0b93b0e
new pdf file for changed
dura-amar Jan 25, 2025
d2d5e90
Testing existing code
dura-amar Feb 24, 2025
a338bc7
Added test data file for GsubWorkerForDevanagari and testing code for…
dura-amar Mar 1, 2025
96cb5f9
Added test data files from excel
dura-amar Mar 1, 2025
5417512
Test code is made less redundant into single function, using paramete…
dura-amar Mar 1, 2025
0daeaeb
Completed test with test data
dura-amar Mar 1, 2025
06640ba
Server added and test case updated
Ayush-Bhandari-772417 May 12, 2025
8b389d4
Merge branch 'trunk' into ayush
dura-amar Jul 9, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions examples/src/main/java/org/apache/pdfbox/examples/LocalPdfServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package org.apache.pdfbox.examples;

import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;

import java.io.*;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.util.*;

public class LocalPdfServer {
public static void main(String[] args) throws IOException {
int port = 8080;
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
System.out.println("Server started at: http://localhost:" + port);

server.createContext("/generate-pdf", new PdfGeneratorHandler());
server.setExecutor(null);
server.start();
}

static class PdfGeneratorHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
if ("OPTIONS".equals(exchange.getRequestMethod())) { // Handle preflight request
exchange.getResponseHeaders().set("Access-Control-Allow-Origin", "*");
exchange.getResponseHeaders().set("Access-Control-Allow-Methods", "POST, OPTIONS");
exchange.getResponseHeaders().set("Access-Control-Allow-Headers", "Content-Type");
exchange.sendResponseHeaders(204, -1); // No content for preflight
return;
}

if (!"POST".equals(exchange.getRequestMethod())) {
exchange.sendResponseHeaders(405, 0);
exchange.close();
return;
}

// Enable CORS for normal requests
exchange.getResponseHeaders().set("Access-Control-Allow-Origin", "*");
exchange.getResponseHeaders().set("Content-Type", "application/pdf");

// Read request body
InputStream inputStream = exchange.getRequestBody();
String requestBody = new String(inputStream.readAllBytes());

// Extract text and font from request
String text = requestBody.replaceAll(".*\"text\":\"(.*?)\".*", "$1");
String fontKey = requestBody.replaceAll(".*\"font\":\"(.*?)\".*", "$1");

if (text.isEmpty()) {
exchange.sendResponseHeaders(400, 0);
exchange.getResponseBody().close();
return;
}

// Get font path
Map<String, String> fonts = getFontMap();
String fontPath = fonts.getOrDefault(fontKey, fonts.get("kalimati")); // Default to Kalimati

File pdfFile = new File("generated.pdf");
generatePDF(text, pdfFile, fontPath);

byte[] pdfData = Files.readAllBytes(pdfFile.toPath());
exchange.sendResponseHeaders(200, pdfData.length);
exchange.getResponseBody().write(pdfData);
exchange.getResponseBody().close();
}
}

private static void generatePDF(String text, File pdfFile, String fontPath) throws IOException {
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);

PDPageContentStream contentStream = new PDPageContentStream(document, page);
PDType0Font pdfFont = PDType0Font.load(document, new File(fontPath));

float startX = 50, startY = 700, fontSize = 14, leading = 1.5f * fontSize;

contentStream.beginText();
contentStream.setFont(pdfFont, fontSize);
contentStream.newLineAtOffset(startX, startY);

List<String> wrappedText = wrapText(text, pdfFont, fontSize, page.getMediaBox().getWidth() - 2 * startX);
for (String line : wrappedText) {
contentStream.showText(line);
contentStream.newLineAtOffset(0, -leading);
}

contentStream.endText();
contentStream.close();
document.save(pdfFile);
document.close();
}

private static Map<String, String> getFontMap() {
Map<String, String> fontMap = new LinkedHashMap<>();
fontMap.put("noto", "examples/src/main/resources/org/apache/pdfbox/resources/ttf/NotoSansDevanagariRegular.ttf");
fontMap.put("noto_the_group", "examples/src/main/resources/org/apache/pdfbox/resources/ttf/NotoTheGroup.ttf");
fontMap.put("kokila", "examples/src/main/resources/org/apache/pdfbox/resources/ttf/Kokila.ttf");
fontMap.put("nirmala", "examples/src/main/resources/org/apache/pdfbox/resources/ttf/Nirmala.ttf");
fontMap.put("nirmala_the_group", "examples/src/main/resources/org/apache/pdfbox/resources/ttf/NirmalaTheGroup.ttf");
fontMap.put("mangal", "examples/src/main/resources/org/apache/pdfbox/resources/ttf/MangalRegular.ttf");
fontMap.put("lohit", "examples/src/main/resources/org/apache/pdfbox/resources/ttf/LohitDevanagari.ttf");
fontMap.put("tiro", "examples/src/main/resources/org/apache/pdfbox/resources/ttf/TiroDevanagariHindiRegular.ttf");
fontMap.put("kalimati", "examples/src/main/resources/org/apache/pdfbox/resources/ttf/Kalimati.ttf");
fontMap.put("kanjirowa", "examples/src/main/resources/org/apache/pdfbox/resources/ttf/Kanjirowa.ttf");
return fontMap;
}

private static List<String> wrapText(String text, PDType0Font font, float fontSize, float maxWidth) throws IOException {
List<String> lines = new ArrayList<>();
String[] words = text.split(" ");
StringBuilder currentLine = new StringBuilder();

for (String word : words) {
String testLine = currentLine.length() == 0 ? word : currentLine + " " + word;
float textWidth = font.getStringWidth(testLine) / 1000 * fontSize;

if (textWidth > maxWidth) {
lines.add(currentLine.toString());
currentLine = new StringBuilder(word);
} else {
currentLine.append(currentLine.length() == 0 ? word : " " + word);
}
}

if (currentLine.length() > 0) {
lines.add(currentLine.toString());
}

return lines;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package org.apache.pdfbox.examples.pdmodel;


import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;

import java.io.File;
import java.io.IOException;
import java.util.*;

/*
* @author Amar Dura
*/
public class NepaliDevanagariPdfGeneration {

public static void main(String[] args) {
PDDocument document;
PDPageContentStream contentStream;
String text = getStringText();
// Load the font file
Map<String,String> fonts = getFontMap();

// Define initial position
float startX = 50;
float startY = 700;
float fontSize = 12;
float leading = 1.5f * fontSize;
try{
document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
contentStream = new PDPageContentStream(document, page);
List<String> fontNames = new ArrayList<>(fonts.keySet());
for (int i = 0; i < fonts.size(); i++) {
String fontName = fontNames.get(i);
String fontPath = fonts.get(fontName);

File fontFile = new File(fontPath);
PDType0Font pdfFont = PDType0Font.load(document, fontFile);

// Begin the contentStream
contentStream.beginText();
contentStream.newLineAtOffset(startX, startY);
contentStream.setFont(pdfFont, fontSize);

// Show font name
contentStream.showText(fontName);
// Move down for spacing (e.g., 2x the leading)
contentStream.newLineAtOffset(0, -1 * leading);

// Split and show the text
List<String> wrappedText = wrapText(text, pdfFont, fontSize, page.getMediaBox().getWidth() - 2 * startX);
for (String line : wrappedText) {
contentStream.showText(line);
contentStream.newLineAtOffset(0, -leading);
}

contentStream.endText();

// Update the startY position for the next font block
startY -= wrappedText.size() * leading + 1.5 * leading; // Extra gap between font blocks
}

contentStream.close();
document.save("examples/src/main/resources/org/apache/pdfbox/examples/nepali/nepali.pdf");
System.out.println("PDF created successfully.");
document.close();


}
catch (IOException e){
System.out.println("PROBLEM: "+e.getMessage());
}
}

private static Map<String, String> getFontMap() {
String noto = "examples/src/main/resources/org/apache/pdfbox/resources/ttf/NotoSansDevanagariRegular.ttf";
String noto_the_group = "examples/src/main/resources/org/apache/pdfbox/resources/ttf/NotoTheGroup.ttf";
String kokila = "examples/src/main/resources/org/apache/pdfbox/resources/ttf/Kokila.ttf";
String nirmala = "examples/src/main/resources/org/apache/pdfbox/resources/ttf/Nirmala.ttf";
String nirmala_the_group = "examples/src/main/resources/org/apache/pdfbox/resources/ttf/NirmalaTheGroup.ttf";
String mangal = "examples/src/main/resources/org/apache/pdfbox/resources/ttf/MangalRegular.ttf";
String lohit = "examples/src/main/resources/org/apache/pdfbox/resources/ttf/LohitDevanagari.ttf";
String tiro = "examples/src/main/resources/org/apache/pdfbox/resources/ttf/TiroDevanagariHindiRegular.ttf";
String kalimati ="examples/src/main/resources/org/apache/pdfbox/resources/ttf/Kalimati.ttf";
String kanjirowa ="examples/src/main/resources/org/apache/pdfbox/resources/ttf/Kanjirowa.ttf";



Map<String, String> fontMap = new LinkedHashMap<>();
// fontMap.put("मङ्गल", mangal);
// fontMap.put("नोटो सान्स देवनागरी", noto);
fontMap.put("लोहित",lohit);
fontMap.put("कालिमाटी", kalimati);
// fontMap.put("निर्मला पुरानो", nirmala);
// fontMap.put("निर्मला नयाँ", nirmala_the_group);

return fontMap;
}
private static List<String> wrapText(String text, PDType0Font font, float fontSize, float maxWidth) throws IOException {
List<String> lines = new ArrayList<>();
String[] words = text.split(" ");
StringBuilder currentLine = new StringBuilder();

for (String word : words) {
String testLine = currentLine.length() == 0 ? word : currentLine + " " + word;
float textWidth = font.getStringWidth(testLine) / 1000 * fontSize;

if (textWidth > maxWidth) {
lines.add(currentLine.toString());
currentLine = new StringBuilder(word);
} else {
currentLine.append(currentLine.length() == 0 ? word : " " + word);
}
}

if (currentLine.length() > 0) {
lines.add(currentLine.toString());
}

return lines;
}

private static String getStringText(){

String loclText = "ख झ ५ ८ ९";
String akhnText ="क्ष त्र ज्ञ";
String rphfText = "र्य र्त्य र्त्स्य र्यि र्यी र्त्स्यि";
String rkrfText = "क्र प्र श्र क्ष्र त्र ज्ञ्र";
String blwfText = "ङ्र ट्र ठ्र ड्र ढ्र";
String halfText = "क्य ख्य ग्य छ्य थ्य ष्ट न्थ्य क्र्क र्क्क";
String cjctText = "ङ्क ङ्क्त ट्क ड्म द्ध द्म द्द द्द्र";
String presText = "क्क क्त क्न ग्न च्च ष्ट्र ल्ल";
String abvsText ="काँ किँ कीँ केँ कैँ कोँ कौँ र्काँ र्किँ र्कीँ र्केँ र्कैँ र्कोँ र्कौँ ";
String blwsText = "रु रू ट्रु ट्रू ङ्कु";
String pstsText ="की खी गी झी";
String halnText = "द् ट् ढ् ड्";

String textOnPdf = "वर्त्स्य टर्कि गर्छन् सङ्क्षिप्त निर् ";

return textOnPdf ;
}
}
Loading