Skip to Content
0
Aug 26, 2022 at 08:16 AM

txt to PDF conversion with desired font in SAP CPI.

331 Views Last edit Dec 11, 2022 at 05:12 AM 3 rev

Hi All,

I'm working on a requirement, where I'm getting a text file which i need to convert into PDF with a Desired font,

in which I have used iText txttoPDF jars. I was able to convert into PDF and produce the below fonts.

TimesNewRoman, Helvetica,Courier,Times-BOLD,

but I'm not able to get any Custom fonts(System-fonts). in my requirement I need to get the Tahoma Font.

below is the code i have used.

import com.sap.gateway.ip.core.customdev.util.Message

import com.itextpdf.io.font.constants.StandardFonts;

import com.itextpdf.kernel.font.PdfFont;

import com.itextpdf.kernel.font.PdfFontFactory;

import com.itextpdf.kernel.pdf.PdfDocument;

import com.itextpdf.kernel.pdf.PdfWriter;

import com.itextpdf.layout.Document;

import com.itextpdf.layout.element.Paragraph;

import com.itextpdf.layout.element.Text;

import java.io.File;

import java.io.IOException;

def Message processData(Message message) {

OutputStream out = new ByteArrayOutputStream()

PdfWriter writer = new PdfWriter(out)

PdfDocument pdfDoc = new PdfDocument(writer)

Document document = new Document(pdfDoc)

def body = message.getBody(java.lang.String) as String

// PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);

PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);

// Text title = body.setFont(font);

Text title = new Text(body).setFont(font);

// def fntSize = 6.7f;

document.add(new Paragraph(title))

// document.add(new Paragraph(body,FontFactory.getFont(FontFactory.COURIER, fntSize)))

document.close()

// writer.close()

message.setHeader('Content-Type', 'application/pdf')

message.setBody(out.toByteArray())

return message

}

Please kindly help me if anybody has a clue or suggestion to use any other Jars or scripts to convert the txt to PDF with desired font in CPI.

Thank you,

Bhagavan Ch