on 06-30-2008 9:37 AM
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.
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.*;
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") );
}
User | Count |
---|---|
95 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.