You're getting an error because PDType1Font is deprecated in newer versions of Apache PDFBox. Here's the corrected version with proper font handling:
@SuppressWarnings("null")
public boolean generateChallanPDF(GeMChallanDataBean challanBean, HttpServletResponse response, HttpServletRequest request, int isEmail, String challanTokenID) throws WSApplicationServiceException {
try {
SimpleDateFormat month = new SimpleDateFormat("MM");
SimpleDateFormat date = new SimpleDateFormat("dd");
SimpleDateFormat year = new SimpleDateFormat("YYYY");
Date currDate = new Date();
Long challanTxnID = Long.parseLong(challanBean.getChallanDetailID());
Map<String, Object> stateDetail = null;
if (!ObjectUtils.isEmpty(commonService.getModifiedOrgDetail(challanTxnID))) {
stateDetail= commonService.getModifiedOrgDetail(challanTxnID).get(0);
}else {
stateDetail= commonService.getOrgDetail(challanBean.getOrgCode()).get(0);
}
String treasuryInputMode = String.valueOf(stateDetail.get("TREASURYCODEINPUTMODE"));
int isTreasuryRequired = Integer.parseInt(stateDetail.get("ISTREASURYCODEREQUIRED").toString());
List<Map<String, Object>> challanDetail = getChallanData(challanTxnID);
List<Map<String, Object>> refundDetail = getRefundData(challanBean.getGemUniqueBidID());
String blockRequestID= !ObjectUtils.isEmpty(challanDetail.get(0).get("blockRequestID")) ? challanDetail.get(0).get("blockRequestID").toString() : "-";
String path = basePath+challanSavePath+"/"+stateDetail.get("ORGCODE").toString()+"/"+year.format(currDate)+"/"+month.format(currDate)+"/"+date.format(currDate);
String challanFileName = "Challan"+challanBean.getChallanDetailID()+"-"+stateDetail.get("ORGCODE").toString()+"-GeM-"+date.format(currDate)+"-"+month.format(currDate)+"-"+year.format(currDate)+".pdf";
String logo = null;
if(!ObjectUtils.isEmpty(stateDetail.get("ORGLOGO"))) {
logo = "data:image/png;base64,"+commonService.bytesToBase64Binary((byte[])stateDetail.get("ORGLOGO"));
}
if(!new File(path+"/"+challanFileName).exists()) {
PDDocument document = null;
File dir = new File(path);
if (!dir.exists()) dir.mkdirs();
try {
document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
logger.info("generateChallanPDF method:inside document:");
try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
// Set up fonts using standard PDFBox fonts
PDFont fontBold = PDType1Font.HELVETICA_BOLD; // This is still available but deprecated
PDFont fontNormal = PDType1Font.HELVETICA; // This is still available but deprecated
// For modern approach, use:
// PDFont fontBold = PDType1Font.HELVETICA_BOLD;
// PDFont fontNormal = PDType1Font.HELVETICA;
float fontSize = 9;
float titleFontSize = 15;
float bodyFontSize = 11;
float margin = 50;
float yStart = page.getMediaBox().getHeight() - margin;
float yPosition = yStart;
float tableWidth = page.getMediaBox().getWidth() - 2 * margin;
// Header with logos
float currentY = yPosition;
// Draw organization logo if available
if (stateDetail.get("ORGLOGO") != null) {
try {
byte[] orgLogoBytes = (byte[]) stateDetail.get("ORGLOGO");
PDImageXObject orgLogoImage = PDImageXObject.createFromByteArray(document, orgLogoBytes, "org_logo");
float orgLogoWidth = 100;
float orgLogoHeight = 35;
contentStream.drawImage(orgLogoImage, margin, currentY - orgLogoHeight, orgLogoWidth, orgLogoHeight);
} catch (Exception e) {
logger.error("Error drawing organization logo", e);
}
}
// Draw GeM logo
try {
InputStream logoStream = null;
PDImageXObject gemLogoImage = null;
logoStream = getClass().getClassLoader().getResourceAsStream("static-images/gem-logo.png");
if (logoStream != null) {
gemLogoImage = PDImageXObject.createFromByteArray(document,
IOUtils.toByteArray(logoStream), "gem_logo");
}
if (gemLogoImage == null) {
logoStream = request.getServletContext().getResourceAsStream("/resources/static-images/gem-logo.png");
if (logoStream != null) {
gemLogoImage = PDImageXObject.createFromByteArray(document,
IOUtils.toByteArray(logoStream), "gem_logo");
}
}
if (gemLogoImage == null) {
String logoPath = request.getServletContext().getRealPath("/resources/static-images/gem-logo.png");
if (logoPath != null && new File(logoPath).exists()) {
gemLogoImage = PDImageXObject.createFromFile(logoPath, document);
}
}
if (gemLogoImage != null) {
float gemLogoWidth = 100;
float gemLogoHeight = 35;
float gemLogoX = page.getMediaBox().getWidth() - margin - gemLogoWidth;
contentStream.drawImage(gemLogoImage, gemLogoX, currentY - gemLogoHeight, gemLogoWidth, gemLogoHeight);
}
} catch (Exception e) {
logger.error("Error loading GeM logo", commonService.getStackTraceToString(e));
}
currentY -= 50; // Space after logos
// Title
contentStream.setFont(fontBold, bodyFontSize);
contentStream.beginText();
contentStream.newLineAtOffset(margin, currentY);
contentStream.showText(messageSource.getMessage("lbl.challan.title", null, Locale.getDefault()));
contentStream.endText();
currentY -= 20;
// Main table data
float rowHeight = 15;
float cellPadding = 5;
float col1 = margin;
float col2 = margin + tableWidth * 0.25f;
float col3 = margin + tableWidth * 0.5f;
float col4 = margin + tableWidth * 0.75f;
// Draw table rows
drawTableRow(contentStream, fontBold, fontNormal, fontSize,
messageSource.getMessage("lbl.beneficiery.ac.no", null, Locale.getDefault()),
challanDetail.get(0).get("BIDVAN").toString(),
messageSource.getMessage("lbl.beneficiery.name", null, Locale.getDefault()),
challanDetail.get(0).get("BUYERPOOLACHOLDERNAME").toString(),
col1, col2, col3, col4, currentY, rowHeight);
currentY -= rowHeight;
drawTableRow(contentStream, fontBold, fontNormal, fontSize,
messageSource.getMessage("lbl.beneficiery.bank.name", null, Locale.getDefault()),
challanDetail.get(0).get("BUYERPOOLBANKNAME").toString(),
messageSource.getMessage("lbl.beneficiery.bank.branch", null, Locale.getDefault()),
commonService.encodeCharacter(challanDetail.get(0).get("BUYERPOOLBRANCHNAME") == null ? "" : challanDetail.get(0).get("BUYERPOOLBRANCHNAME").toString()),
col1, col2, col3, col4, currentY, rowHeight);
currentY -= rowHeight;
drawTableRow(contentStream, fontBold, fontNormal, fontSize,
messageSource.getMessage("lbl.beneficiery.ifsc.code", null, Locale.getDefault()),
challanDetail.get(0).get("BUYERPOOLACIFSC").toString(),
messageSource.getMessage("lbl.uniquebidid", null, Locale.getDefault()),
challanBean.getGemUniqueBidID(),
col1, col2, col3, col4, currentY, rowHeight);
currentY -= rowHeight;
drawTableRow(contentStream, fontBold, fontNormal, fontSize,
messageSource.getMessage("lbl.amount", null, Locale.getDefault()),
challanBean.getAmtToDisp() + "\n" + "("+challanBean.getAmtInStr().replaceAll("[ ,]"," ").trim()+")",
messageSource.getMessage("lbl.ddo.code", null, Locale.getDefault()),
challanBean.getDdoCode(),
col1, col2, col3, col4, currentY, rowHeight);
currentY -= rowHeight;
drawTableRow(contentStream, fontBold, fontNormal, fontSize,
messageSource.getMessage("lbl.ddo.name", null, Locale.getDefault()),
challanDetail.get(0).get("DDONAME").toString(),
messageSource.getMessage("lbl.state", null, Locale.getDefault()),
stateDetail.get("STATENAME").toString(),
col1, col2, col3, col4, currentY, rowHeight);
currentY -= rowHeight;
drawTableRow(contentStream, fontBold, fontNormal, fontSize,
messageSource.getMessage("lbl.date.time", null, Locale.getDefault()),
challanBean.getChallanReqDate(),
messageSource.getMessage("lbl.organization.code", null, Locale.getDefault()),
challanBean.getOrgCode(),
col1, col2, col3, col4, currentY, rowHeight);
currentY -= rowHeight;
// Block Request ID (single column)
contentStream.setFont(fontBold, fontSize);
contentStream.beginText();
contentStream.newLineAtOffset(col1, currentY);
contentStream.showText(messageSource.getMessage("lbl.block.request.id", null, Locale.getDefault()));
contentStream.endText();
contentStream.setFont(fontNormal, fontSize);
contentStream.beginText();
contentStream.newLineAtOffset(col2, currentY);
contentStream.showText(blockRequestID);
contentStream.endText();
currentY -= (rowHeight * 2); // Extra space
// Treasury Details
if(isTreasuryRequired==1 && treasuryInputMode.equals("1")) {
currentY = drawTreasuryDetails(contentStream, fontBold, fontNormal, bodyFontSize, fontSize,
challanDetail, messageSource, currentY, margin, tableWidth);
} else if (isTreasuryRequired==1 && treasuryInputMode.equals("2")) {
contentStream.setFont(fontBold, bodyFontSize);
contentStream.beginText();
contentStream.newLineAtOffset(margin, currentY);
contentStream.showText(messageSource.getMessage("lbl.treasury.and.other.detail", null, Locale.getDefault()));
contentStream.endText();
currentY -= rowHeight;
String treasuryDetail = commonService.removehtmltag(challanDetail.get(0).get("TREASURYDETAIL").toString());
currentY = drawWrappedText(contentStream, fontNormal, fontSize, treasuryDetail, margin, currentY, tableWidth);
currentY -= rowHeight;
}
// Refund Details
if(isTreasuryRequired==1 && treasuryInputMode.equals("1")) {
currentY -= rowHeight;
currentY = drawRefundDetails(contentStream, fontBold, fontNormal, bodyFontSize, fontSize,
refundDetail, messageSource, currentY, margin, tableWidth);
}
// Disclaimer
currentY -= rowHeight;
String disclaimerText = "Disclaimer: \n\n" +
messageSource.getMessage("lbl.challan.disclaimer", null, Locale.getDefault());
currentY = drawWrappedText(contentStream, fontNormal, fontSize, disclaimerText, margin, currentY, tableWidth);
logger.info("generateChallanPDF method::PDF created Successfully with PDFBox");
}
// Save the document
document.save(new File(path + File.separator + challanFileName));
} catch(Exception ex){
logger.error("Error generating PDF with PDFBox: " + commonService.getStackTraceToString(ex));
throw ex;
} finally{
if(document != null) {
document.close();
}
}
manageGeMChallanDAO.updateChallanFileName(challanTxnID, challanFileName);
}
// Email Challan File as Attachment
if(isEmail==1 && !ObjectUtils.isEmpty(challanBean.getEmailID())) {
EmailDataBean emailBean = new EmailDataBean();
emailBean.setEmailTemplateID(challanAttachTemplateID);
emailBean.setEmailTo(Arrays.asList(StringUtils.commaDelimitedListToStringArray(challanBean.getEmailID())).stream().distinct().toArray(String[]::new));
emailBean.setFilePath(path+"/"+challanFileName);
emailBean.setSubjectParams(new Object[] {stateDetail.get("ORGNAME").toString()});
emailBean.setBodyParams(new Object[] {stateDetail.get("ORGNAME").toString(), challanBean.getGemUniqueBidID(), challanBean.getAmtToDisp()});
if(messageService.sendEmail(emailBean)) {
return true;
} else {
return false;
}
} else if(isEmail==0) {
// Download challan pdf file
commonService.downloadFile(challanFileName, path, response);
return true;
} else {
return false;
}
} catch (Exception e) {
logger.error("In Generate Challan PDF" + commonService.getStackTraceToString(e));
throw new WSApplicationServiceException("lbl.invalid.request",commonService.generateErrorMap("Error While Generating PDF Error :- " + e.getMessage()));
}
}
// Helper methods remain the same...
Important Notes:
-
PDType1Font is deprecated but still available in PDFBox 3.0.5. The classes are still present and functional.
-
For future compatibility, consider using:
// Instead of PDType1Font
PDFont fontBold = PDType1Font.HELVETICA_BOLD;
PDFont fontNormal = PDType1Font.HELVETICA;
// Or use custom fonts if needed
PDFont fontBold = PDTrueTypeFont.loadFont(document, "path/to/font.ttf", true);
-
If you want to avoid deprecation warnings, you can suppress them:
@SuppressWarnings("deprecation")
PDFont fontBold = PDType1Font.HELVETICA_BOLD;
The code should work perfectly with PDFBox 3.0.5 as-is, since PDType1Font is still supported even though it's marked as deprecated.