cancel
Showing results for 
Search instead for 
Did you mean: 

non based forms PDF files

Former Member
0 Kudos

Hi all,

Anybody could help me in converting dinamic tables, textfields, etc from an IVIEW to a pdf document without using any forms...??

thanks in advance

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Ischia,

if you don't want to use PDF Interactive Forms perhaps this project should be useful for you.

Project <b>FOP</b> (http://xmlgraphics.apache.org/fop/) to export any context node in PDF format, after importing and using a plugin.

Hope this help you.

Gianluca

Former Member
0 Kudos

Hi Gianluca;

Thanks a lot for you replay. The solution you propose is very innovative. It will be a challenge!

I'm already working in that. Let's see how it ends...

Last comment: your idea makes me feel like if it wasn't possible to convert Iviews contents into a pdf file without using forms. Maybe webdynpro does not support that functionality.

Does everybody agree with me or have any solution?

Former Member
0 Kudos

Hi Ivan,

I've tested the FOP project by myself into WD application and I could say that it's a valid alternative to Interactive Forms.

The only issue is that there isn't a UI control for FOP in NWDS so you must insert the plugin into WD project and add some code to use it.

If you like this solution, can you give me some forum points please. I appreciate...

Regards.

Gianluca Barile

Former Member
0 Kudos

Grazie gianluca!,

I also think it would be a fine alternative, even thought i haven't taken a deep investigation on it, since I didn't found out how to insert the uncompressed .tar.gz file I downloaded into a WD plugin.

Could you guide me please in more detail steps, including an example of the code you used?

Forum points for you thanks!

PD: If you find useful to use my email address please do it: alfredogarciarios@gmail.com

Former Member
0 Kudos

Hi Ivan,

you must add all files of the FOP under the lib folder in Navigator Perspective.

The files are in the attachment that I sent you by email.

this is an example of Java function that use FOP:

public int CreaFilePdf( )
  {
    //@@begin CreaFilePdf()
	////////////////////////////
	int ret = 0;
	byte[] pdfFoXMLFile = null;
	byte[] pdfFile;
	IWDCachedWebResource cachedPdfResource = null;
	String fileName = new String();
	fileName = "Equipment.pdf";
	String foText = new String();

	////////////////////////////
	try {
		foText = this.toFoXML();
		wdContext.currentContextElement().setFoText(foText);
		pdfFoXMLFile = foText.getBytes("UTF-8");
		
		
		//fileName = "Equipment.pdf";
		
		OutputStream outFo = new java.io.FileOutputStream("Equipment.pdf.fo");
		
		try {
			outFo.write(pdfFoXMLFile);
			outFo.flush();
			outFo.close();
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		/////////////fo to PDF
//		Construct driver
	  	Driver driver = new Driver();

	  	//Setup logger
	  	Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
	  	driver.setLogger(logger);
	  	MessageHandler.setScreenLogger(logger);

	  	//Setup Renderer (output format)        
  		driver.setRenderer(Driver.RENDER_PDF);
		  
		OutputStream out = new java.io.FileOutputStream("EquipmentFromFo.pdf");
		try {
			driver.setOutputStream(out);

			//Setup input
			InputStream in = new java.io.FileInputStream("Equipment.pdf.fo");
			try {
				driver.setInputSource(new InputSource(in));
            
				//Process FO
				driver.run();
			} finally {
				in.close();
			}
		} finally {
			out.close();
		}
		/////////////fo to PDF		
///////////////////////////////web resource


  try {
	// create Excel 2003 XML data as a byte array for the given context node, attributes and headers  
	
	
	// Deserialize from a file
	File file = new File("EquipmentFromFo.pdf");

	// Get some byte array data
	pdfFile = getBytesFromFile(file);
	
	
	//pdfFoXMLFile = this.toFoXML().getBytes("UTF-8");
	// create a cached Web Dynpro XLS Resource for the given byte array and filename
	
	cachedPdfResource = this.getCachedWebResource(pdfFile, fileName, WDWebResourceType.PDF);

	// Store URL and filename of cached excel resource in context. 
	if (cachedPdfResource != null) {
	  wdContext.currentContextElement().setPdfFileURL(cachedPdfResource.getURL());
	  wdContext.currentContextElement().setPdfFileName(cachedPdfResource.getResourceName());

	} else {
	  ret = 1;
	}
  }
  catch (WDURLException e) {
	  ret = 1;
  }
  
  
///////////////////////////////
	} catch (FileNotFoundException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} catch (IOException e2) {
		// TODO Auto-generated catch block
		e2.printStackTrace();
	} catch (FOPException e3) {
		// TODO Auto-generated catch block
		e3.printStackTrace();
	}
    

	

    
    /////////////////////////////
	return ret;
    //@@end
  }

If RET is 0 the generation is OK!!

Regards.

Gianluca

Former Member
0 Kudos

Hello Gianluca, great job!

I must ask you a last question, since I have used your code and I get some errors.

Could you please be so nice to tell me all imports stataments? And also from which context do you write this method CreaFilePdf()?

I say it because I'm getting this erros also:

this.toFoXMK

this.getBytesFromFile

this.getCachedWebResorce

Thansk very much,

Ivan

Former Member
0 Kudos

Hi Ivan,

I post you the code.

METHOD

toFoXML

 private String toFoXML() {
	StringBuffer x = new StringBuffer();
	String Text = new String("");
	int dimCol = 0;
	String ImgUrl = new String();

	x.append("<?xml version='1.0' encoding='utf-8'?>n");
	x.append("<root xmlns='http://www.w3.org/1999/XSL/Format' font-family='Times' font-size='20pt'>n");

	x.append("   <layout-master-set>n");
	x.append("      <simple-page-master master-name='frame' page-height='21cm' page-width='29.7cm' margin-top='3mm' margin-bottom='3mm' margin-left='3mm' margin-right='3mm'>n");
	x.append("         <region-body region-name='frame-body' margin-top='1mm' margin-bottom='1mm'></region-body>n");
	x.append("         <region-before region-name='frame-before' extent='1mm'></region-before>n");
	x.append("         <region-after region-name='frame-after' extent='1mm'></region-after>n");
	x.append("      </simple-page-master>n");
	x.append("   </layout-master-set>n");
	x.append("   <fo:page-sequence xmlns:fo='http://www.w3.org/1999/XSL/Format' master-reference='frame' >n");

	x.append("<fo:flow flow-name='frame-body'>n");
	x.append("<fo:block space-after.optimum='10pt' font-family='Helvetica' font-size='10pt'>n");

	try {
		WDDeployableObjectPart part = wdComponentAPI.getDeployableObjectPart();
		ImgUrl = WDURLGenerator.getAbsoluteWebResourceURL(part, "siemens.gif");
	} catch (WDURLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	x.append("<fo:external-graphic  width="63pt" height="10pt"  src="url('"+ImgUrl+"')"></fo:external-graphic>");

	x.append("<fo:table  border-top="solid black 1px" border-left="solid black 1px">n");
	
	//  COLONNA DEFAULT per EQUIPMENT
	dimCol = 10;
	dimCol = 11*dimCol;
	x.append("<fo:table-column column-width='");
	x.append(dimCol);
	x.append("pt'></fo:table-column>n");
		
	wdContext.nodeColumnSelezionati().moveFirst();
	do {
		Text = wdContext.currentColumnSelezionatiElement().getText();
		Text = Text.substring(Text.lastIndexOf("(")+1,Text.length()-1);
		
		dimCol = new Integer(Text).intValue();
		
		Text = wdContext.currentColumnSelezionatiElement().getText();
		Text = Text.substring(0, Text.lastIndexOf("("));
		
		if (dimCol < Text.length())
			dimCol = Text.length();
		
		dimCol = 5*dimCol;
		
		x.append("<fo:table-column column-width='");
		x.append(dimCol);
		x.append("pt'/>n");
	}while(wdContext.nodeColumnSelezionati().moveNext() != null);
	
	x.append("<fo:table-body>n");
	x.append("<fo:table-row>n");

	//  COLONNA DEFAULT per EQUIPMENT
	x.append("<fo:table-cell  padding-top="1pt" padding-left="1pt"  border-bottom="solid black 1px" border-right="solid black 1px" >n");
	x.append("  <fo:block >n");
	x.append("Equipment");
	x.append("</fo:block>n");
	x.append("</fo:table-cell>n");
	
	
	//intestazioni colonne
	wdContext.nodeColumnSelezionati().moveFirst();
		do {
			Text = wdContext.currentColumnSelezionatiElement().getText();
			Text = Text.substring(0,Text.lastIndexOf("("));
			x.append("<fo:table-cell  padding-top="1pt" padding-left="1pt"  border-bottom="solid black 1px" border-right="solid black 1px" >n");
			x.append("  <fo:block >n");
			x.append(Text);
			x.append("</fo:block>n");
			x.append("</fo:table-cell>n");
		}while(wdContext.nodeColumnSelezionati().moveNext() != null);
		
	x.append("</fo:table-row>n");

	//contenuto colonne
	wdContext.nodeIt_Equnr().moveFirst();
	do{
		//individuo se è una main oppure una sub tramite il campo lev
		Text = wdContext.currentIt_EqunrElement().getAttributeAsText("Lev");
		if (Text.equals("MAIN")){
			x.append("<fo:table-row>n");
			
					//		COLONNA DEFAULT per EQUIPMENT
					x.append("<fo:table-cell   padding-top="1pt" padding-left="1pt"  border-bottom="solid black 1px" border-right="solid black 1px" >n");
					x.append("  <fo:block >n");
					Text = wdContext.currentIt_EqunrElement().getEquipment();
		
					while (Text.charAt(0) == '0')
						Text = Text.substring(1);
			
					x.append(Text);
					x.append("</fo:block>n");
					x.append("</fo:table-cell>n");
		}else{
			x.append("<fo:table-row>n");
			//		COLONNA DEFAULT per EQUIPMENT
					x.append("<fo:table-cell   padding-top="1pt" padding-left="6pt"  border-bottom="solid black 1px" border-right="solid black 1px" >n");
					x.append("  <fo:block >n");
					Text = wdContext.currentIt_EqunrElement().getEquipment();
		
					while (Text.charAt(0) == '0')
						Text = Text.substring(1);
			
					x.append(Text);
					x.append("</fo:block>n");
					x.append("</fo:table-cell>n");
		}
		
		wdContext.nodeColumnSelezionati().moveFirst();
		do {
			x.append("<fo:table-cell   padding-top="1pt" padding-left="1pt"  border-bottom="solid black 1px" border-right="solid black 1px" >n");
			x.append("  <fo:block >n");
			Text = wdContext.currentColumnSelezionatiElement().getNameCol();
			Text = wdContext.currentIt_EqunrElement().getAttributeAsText(Text);
			x.append(traduciHTML(Text));
			x.append("</fo:block>n");
			x.append("</fo:table-cell>n");
		}while(wdContext.nodeColumnSelezionati().moveNext() != null);
		x.append("</fo:table-row>n");
	}while(wdContext.nodeIt_Equnr().moveNext() != null);
		
	x.append("</fo:table-body>n");
	x.append("</fo:table>n");
	x.append("</fo:block>n");
	
	x.append("</fo:flow>n");
	x.append("</fo:page-sequence>n");
	x.append("</root>n");
 
	wdContext.nodeIt_Equnr().moveFirst();
	wdContext.nodeColumnSelezionati().moveFirst();
	
	return x.toString();
   }  

-

-


METHOD

getBytesFromFile

public static byte[] getBytesFromFile(File file) throws IOException {
		   InputStream is = new FileInputStream(file);
    
		   // Get the size of the file
		   long length = file.length();
    
		   // You cannot create an array using a long type.
		   // It needs to be an int type.
		   // Before converting to an int type, check
		   // to ensure that file is not larger than Integer.MAX_VALUE.
		   if (length > Integer.MAX_VALUE) {
			   // File is too large
		   }
    
		   // Create the byte array to hold the data
		   byte[] bytes = new byte[(int)length];
    
		   // Read in the bytes
		   int offset = 0;
		   int numRead = 0;
		   while (offset < bytes.length
				  && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
			   offset += numRead;
		   }
    
		   // Ensure all the bytes have been read in
		   if (offset < bytes.length) {
			   throw new IOException("Could not completely read file "+file.getName());
		   }
    
		   // Close the input stream and return bytes
		   is.close();
		   return bytes;
	   } 

-

-


METHOD

getCachedWebResource

private IWDCachedWebResource getCachedWebResource(byte[] file, String name, WDWebResourceType type) {
	 IWDCachedWebResource cachedWebResource = null;
	 if (file != null) {
	   cachedWebResource = WDWebResource.getWebResource(file, type);
	   cachedWebResource.setResourceName(name);
	 }
	 return cachedWebResource;
   }

-

-


IMPORT:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.TimeZone;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.FOPException;
import org.apache.fop.messaging.MessageHandler;
import org.xml.sax.InputSource;
import com.sap.tc.webdynpro.services.sal.deployment.api.WDDeployableObjectPart;
import com.sap.tc.webdynpro.services.sal.url.api.IWDCachedWebResource;
import com.sap.tc.webdynpro.services.sal.url.api.WDURLException;
import com.sap.tc.webdynpro.services.sal.url.api.WDURLGenerator;
import com.sap.tc.webdynpro.services.sal.url.api.WDWebResource;
import com.sap.tc.webdynpro.services.sal.url.api.WDWebResourceType;

The method CreaFilePDF writes on a value attribute

foText

in Context Interf. Controller.

Regards.

Gianluca Barile

Former Member
0 Kudos

Hi Ivan,

please, if my suggestion has cleared your doubts don't forget to give me forum points.

Thank you.

Regards.

Gianluca Barile

Former Member
0 Kudos

Hi Gianluca,

Thanks again for your suggested solution.

As I appreciate your help I have already given you all forum point I could.

Ciao, and hope to keeping on some knowledge transfer!

Answers (1)

Answers (1)

Former Member
0 Kudos
Former Member
0 Kudos

Hi Vito thanks for you replay,

Unfortunately the link you suggested guides the reader to use Adobe Interactive Forms, and that's exactly what I'm requested to avoid.

Any other ideas would be very welcame!