on 09-09-2015 8:32 AM
Hi All,
Do we have any standard module to read contents from PDF files or PPT in SAP PO 7.4.
If we need to write Java code, Is it possible to read pdf or ppt file in mapping as a stream and work on? or writing Module will be a best way?
Regards,
Mani
Hi,
Its better to go with a java mapping as you can more easily utilize the free third party API's available on internet..
for reading pdf's one such API is PDFBox i used this API to read a PDF from sender file adapter and converted the InputStream in java mapping....
but the problem in reading pdf's is that this API Pdfbox(this is the only good API free) reads the pdf in a line by line fashion ignoring any structures / tables you have in pdf.... we made a POC for reading a PDF in PI we were able to successfully extract the text say PO Number, Order number etc based on string search but we lost he structures tables etc..... so we abandoned the idea... if you are looking for a small amount of information or very specific information like order number etc then you can do it in PI but for reading complex pdfs you will need API's which are not FREE...
Regards
vinay
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Here goes the actual code
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.HashMap;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.*;
import org.apache.pdfbox.util.*;
import org.apache.pdfbox.pdmodel.*;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
public class PdftoXml extends AbstractTransformation
{
public String from ="", to="", invoiceid ="", ponumber="", issuedate="", duedate="", subject="";
public String subtotal ="", discountpercent ="", discountamount ="", taxpercent ="", taxamount="", finalamount ="", notes="";
String result = "";
public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException
{
PDDocument pd;
BufferedWriter wr;
try {
pd = PDDocument.load(in.getInputPayload().getInputStream()); //convert Tranformationimput to inputstream than pass it to PDDocument constructor to read Pdf from Inputstream.
//System.out.println(pd.getNumberOfPages()); //prints number of pages
PDFTextStripper stripper = new PDFTextStripper();
String str = stripper.getText(pd);
//**********************************************Xml formation************************************
PdftoXml formxml = new PdftoXml();
initializeFields(str);
result = makeXML();
//**********************************************Xml formation end********************************
out.getOutputPayload().getOutputStream().write(result.getBytes("UTF-8")); //writing to output
//System.out.println(result);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public String makeXML()
{
result ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
result = result.concat("<ns0:MTPdf xmlns:ns0=\"namespace\">");
result=result.concat("<parent>");
result=result.concat("\n<HeaderText>Invoice</HeaderText>");
result=result.concat("\n<From>"+from+"</From>");
result=result.concat("\n<To>"+to+"</To>");
result=result.concat("\n<InvoiceId>"+invoiceid+"</InvoiceId>");
result=result.concat("\n<PONumber>"+ponumber+"</PONumber>");
result=result.concat("\n<IssueDate>"+issuedate+"</IssueDate>");
result=result.concat("\n<DueDate>"+duedate+"</DueDate>");
result=result.concat("\n<Subject>"+subject+"</subject>");
result=result.concat("\n<Subtotal>"+subtotal+"</Subtotal>");
result=result.concat("\n<Discount>");
result=result.concat("\n<DiscountPercentage>"+discountpercent+"</DiscountPercentage>");
result=result.concat("\n<DiscountAmount>"+discountamount+"</DiscountAmount>");
result=result.concat("\n</Discount>");
result=result.concat("\n<Tax>");
result=result.concat("\n<TaxPercentage>"+taxpercent+"</TaxPercentage>");
result=result.concat("\n<TaxAmount>"+taxamount+"</TaxAmount>");
result=result.concat("\n</Tax>");
result=result.concat("\n<FinalAmount>"+finalamount+"</FinalAmount>");
result=result.concat("\n<Notes>"+notes+"</Notes>");
result=result.concat("\n</parent>");
result = result.concat("\n</ns0:MTPdf>");
return result;
}
public void initializeFields(String unsorted)
{
String[] lines =unsorted.split("\r\n|\r|\n");
int len = lines.length;
int i =0;
while(i<len)
{
if((lines[i].toLowerCase()).startsWith("subtotal"))
{
subtotal = lines[i].substring(9,lines[i].length());
}
else if((lines[i].toLowerCase()).startsWith("discount"))
{
discountpercent = lines[i].substring(10,lines[i].indexOf("%"));
discountamount = lines[i].substring(lines[i].indexOf("$"),lines[i].length());
}
else if((lines[i].toLowerCase()).startsWith("tax"))
{
taxpercent = lines[i].substring(6,lines[i].indexOf("%"));
taxamount = lines[i].substring(lines[i].indexOf("$"),lines[i].length());
}
else if((lines[i].toLowerCase()).startsWith("amount due"))
{
finalamount = lines[i].substring(lines[i].indexOf("$"),lines[i].length());
}
else if((lines[i].toLowerCase()).startsWith("notes"))
{
notes = lines[i+1];
}
else if((lines[i]).startsWith("INVOICE From"))
{
from = lines[i].substring(13,lines[i].length());
}
else if((lines[i].toLowerCase()).startsWith("invoice for"))
{
to = lines[i].substring(12,lines[i].length());
}
else if((lines[i].toLowerCase()).startsWith("invoice id"))
{
invoiceid = lines[i].substring(11,lines[i].length());
}
else if((lines[i].toLowerCase()).startsWith("po number"))
{
ponumber = lines[i].substring(10,lines[i].length());
}
else if((lines[i].toLowerCase()).startsWith("issue date"))
{
issuedate = lines[i].substring(11,lines[i].length());
}
else if((lines[i].toLowerCase()).startsWith("due date"))
{
duedate = lines[i].substring(9,lines[i].length());
}
else if((lines[i].toLowerCase()).startsWith("subject"))
{
subject = lines[i].substring(8,lines[i].length());
}
i++;
}
}//function intialize end
}
User | Count |
---|---|
84 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.