cancel
Showing results for 
Search instead for 
Did you mean: 

PDF generation from Webdynpro for java application

Former Member
0 Kudos

Hi all,

I have a scenario in which a PDF has to be generated from the datils present in a table in the same view, on click of a button. I have found many solutions for generating interactive forms but I need to generate it from the code directly. I have also found an application for generating an excel sheet from the table data but it is not working with PDF.

So anybody suggest me how to do it or please refer any applications that have been done.

Thanks in advance.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

you can use iText, a java library for PDF generation. The .jar file should be packaged inside the web dynpro or in a separate JEE library (a better solution if you need the PDF generation facility in many places).

http://www.lowagie.com/iText/download.html

Regards

Former Member
0 Kudos

Hi Marco,

Thanks for your idea but i have tried this. I have got the source code for generation of the pdf, but its not working. And moreover it is a bit difficult task to put the data in that PDF.

I have found out how to open a PDF but i am unable put some data into it....

For ex: to generate an excel file we first convert our data to xml format and then we put that in a byte array and then we give this byte array as an input to the excel.

If we try the same for PDF, its not working....

it says data has been damaged....

So please help me in this regard

Former Member
0 Kudos

Hi Srikanth,

It will be easier for me to help you if could you give us some more details on the procedure you follow and maybe even some code snippets.

Former Member
0 Kudos

Hi marco,

Sorry for the late reply.

I have got an application to generate an excel file and i will paste that code here for your reference.

*********************************

byte[] excelXMLFile;

IWDCachedWebResource cachedExcelResource = null;

String fileName = dataNode.getNodeInfo().getName() + ".xls";

try {

// create Excel 2003 XML data as a byte array for the given context node, attributes and headers

excelXMLFile = this.toExcel(dataNode, columnInfos).getBytes("UTF-8");

// create a cached Web Dynpro XLS Resource for the given byte array and filename

cachedExcelResource = this.getCachedWebResource(excelXMLFile, fileName, WDWebResourceType.XLS);

// Store URL and filename of cached excel resource in context.

if (cachedExcelResource != null) {

wdContext.currentContextElement().setExcelFileURL(cachedExcelResource.getURL());

wdContext.currentContextElement().setExcelFileName(cachedExcelResource.getResourceName());

// Open popup window with a link to the cached excel file web resource.

this.openExcelLinkPopup();

} else {

wdComponentAPI.getMessageManager().reportException("Failed to create Excel file from table!", true);

}

} catch (UnsupportedEncodingException e) {

wdComponentAPI.getMessageManager().reportException(e.getLocalizedMessage(), true);

} catch (WDURLException e) {

wdComponentAPI.getMessageManager().reportException(e.getLocalizedMessage(), true);

}

**************************************

In the above code "excelXMLFile" is a byte array in which the data that has to be appended to the pdf (in xml format) is present.

the toExcel method is used to convert the data in the table(i.e., the data that gets populated in the nodes of the view) in to XML format.

My Problem is when i change the filename to .pdf extension and the WDWebResourceType.XLS to WDWebResourceType.PDF and execute the application the Acrobat reader is getting opened but it says

"unsupported file type or the file has been damaged" .

I mean i am unable to put data into the file.

So, please help me in this regard.

Former Member
0 Kudos

Hi Srikanth

Even if you change the file extension and the WDWebResourceType to PDF, the data you generated is still an excel file and Adobe will fail to load it.

To correctly generate a PDF file you should use a PDF library.

You can use iText in the following manner (I simplify a bit and assume you have only two string attribute per row, field1 and field2).



private byte[] toPDF(IWDNode dataNode ) {
  Document document = new Document();
  try {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PdfWriter.getInstance(document, out);
    document.open();
    PdfPTable table = new PdfPTable(2); // number of columns
    table.addCell("Field1"); // column headers
    table.addCell("Field2");
    for(int i = 0; i < dataNode.size(); i++ ) {
      table.addCell( dataNode.getElementAt(i).getAttributeAsText("field1") );
      table.addCell( dataNode.getElementAt(i).getAttributeAsText("field2") );
    }
    document.add(table);
    document.close();
    return out.toByteArray();
  } catch (DocumentException e) {
    wdComponentAPI.getMessageManager().reportException(e.getLocalizedMessage(), true);
  } catch (IOException e) {
    wdComponentAPI.getMessageManager().reportException(e.getLocalizedMessage(), true);
  }
  return null;
}

you should add the iText .jar file to your web dynpro and its buildpath, and then use the following import statement

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.io.*;

Former Member
0 Kudos

Hi Marco,

Thanks for the code. but when i execute it i have a problem.

the exception is:

java.lang.NoClassDefFoundError: com/lowagie/text/Element

I have included itext.jar as u have said, even then the above exception is raised.

The problem may be because of the following code:

for(int i = 0; i < dataNode.size(); i++ ) {
      table.addCell( dataNode.getElementAt(i).getAttributeAsText("field1") );
      table.addCell( dataNode.getElementAt(i).getAttributeAsText("field2") );
    }

Former Member
0 Kudos

Hi Srikanth,

you are able to build but you get that error at runtime? Where exactly did you put the .jar file?

If you prefer you may use a DC of type external library, with two public parts (compilation and assembly) and reference both parts from your web dynpro DC.

Former Member
0 Kudos

Yes Marco,

the error is at runtime. i have added the jar file in java build path->add external libraries.

and one more problem is how to create a method with byte(Simple types) array as its return type, since the array type chk box is not getting highlighted when creating a method.

Former Member
0 Kudos

You should add the library to the lib directory of your web dynpro and then use "add libraries" (not external).

(for the return type you can use "binary")

Former Member
0 Kudos

Hi Morco,

Thanks a lot for the help. Its working. My pdf is getting generated and i can append the field i want.

Once again thanks for the help.

Sorry for not rewarding till now.

Answers (0)