How to resolve pdf parsing error - scala

scala code :
val file = new File(path + name)
val raf = new RandomAccessFile(file, "r")
val channel = raf.getChannel()
val buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size())
val pdffile = new PDFFile(buf) // line 5
here, file is referring to pdf file. path is the address of pdf file and name is name of file.
In normal case, it executes fine, but in some pdf files, it throws error in line 5 as :
com.sun.pdfview.PDFParseException: Expected 'xref' at start of table
at com.sun.pdfview.PDFFile.readTrailer(PDFFile.java:974) ~[pdf-renderer-1.0.5.jar:na]
at com.sun.pdfview.PDFFile.parseFile(PDFFile.java:1175) ~[pdf-renderer-1.0.5.jar:na]
at com.sun.pdfview.PDFFile.<init>(PDFFile.java:126) ~[pdf-renderer-1.0.5.jar:na]
at com.sun.pdfview.PDFFile.<init>(PDFFile.java:102) ~[pdf-renderer-1.0.5.jar:na]
I think this pdf file has some problem with its format or content. When i made another pdf file using save as with this pdf file and used that new created pdf file, it worked fine.
So how can i resolve this problem so that whetehr i use proper file or bad file, my code should work fine.
EDIT
I found the following in the com/sun/pdfview/PDFFile.java description
#throws PDFParseException if the document appears to be malformed, or
its features are unsupported
.

Related

Read Forms from BAR file - Flowable in code -

I need an example of usign FormEngine. To be more especific....
I'm executing code below - but there’s no forms found in my BAR file :(
The BAR file was exported from Flowable Modeler and it contains one form and one process and app. Maybe there's other way to deploy and obtain forms...?
RepositoryService repositoryService = processEngine.getRepositoryService();
FormRepositoryService formRepositoryService = formEngine.getFormRepositoryService();
File file = new File(path);
ZipInputStream inputStream = new ZipInputStream(new FileInputStream(path));
String idDeployParent = repositoryService.createDeployment()
.name(file.getName())
.addZipInputStream(inputStream)
.deploy()
.getId();
DeploymentEntity deploymentEntity = (DeploymentEntity) repositoryService.createDeploymentQuery().list().get(0);
formRepositoryService.createDeployment()
.name(file.getName())
.parentDeploymentId(idDeployParent)
.deploy();
System.out.println(" FORMS FOUND: " + formRepositoryService.createFormDefinitionQuery().list().size());

Ionic 3 Cordova File plugin gives error for copyFile operation

I am trying to copy a file from one dir to another using the copyFile(path, fileName, newPath, newFileName) function. It gives an error like {"code":13, "message":"input is not a directory"}. The documentation has only 12 error code and no 13th. I'd like to know what i did wrong please.
Here is a sample of my actual code.
this.path = "file:///storage/emulated/0/TheFolder/thefile.ext";
this.newPath = "file:///storage/emulated/0/NewFolder";
this.fileCtrl.copyFile(this.path, fileName, this.newPath, newFileName)
this.path must be a directory but your are showing some file name
change your code as follows
this.path = "file:///storage/emulated/0/TheFolder";
this.newPath = "file:///storage/emulated/0/NewFolder";
this.fileCtrl.copyFile(this.path, YOUR_EXISTING_FILE_NAME, this.newPath, NEW_FILE_NAME);
path -Base FileSystem
fileName - Name of file to copy
newPath - Base FileSystem of new location
newFileName - New name of file to copy to (leave blank to remain the same)

Parse a XES file in Scala

I am trying since few hours to parse an XES file using the Deckfour XES Open library, I want to have the logs of my file however I can't find any parser which parse my .xes
//Getting Xes File
val logXesFile = request.body.file("file").get
val filename = logXesFile.filename
logger.debug(s"filename: $filename")
//Try all available parsers
val otherParsers = XParserRegistry.instance().getAvailable
for(p <- otherParsers){
try{
logger.debug(p.name())
val logs = p.parse(logXesFile.ref.file)
if(logs.size() > 0){
logger.debug(s"Parser founded ! $p")
}
}
catch{
case e : Exception => {logger.debug("Exception !")}
}
}
Here is a screenshot of my debugger :
Also I tried plenty of xes files the problem are definitely not from those, I also tried with the GZIP parser
Ok I finally found !
The "myfile".ref.file allow indeed to get the file however it change the name of the file to something like "multipartBody26...TemporaryFile" and the parser function from the deckfour library do not recognize it as a ".xes" anymore while the content is still an xes file.
So the solution looks to be to rename the file

How to create virtual XML for ZUGFeRD Invoices

I try to create a PDF/A-3b file which contains an embedded XML-File to be ZUGFeRD conform. I use Perl and PDFLib for this purpose. The PDFLib Documentation out there is just for Java and PHP. Creating the PDF works fine, but the XML part is my problem.
So how can i create a pvf from xml and join this to my pdf?
This is what PDFLib recommends in Java:
// Place XML stream in a virtual PVF file
String pvf_name = "/pvf/ZUGFeRD-invoice.xml";
byte[] xml_bytes = xml_string.getBytes("UTF-8");
p.create_pvf(pvf_name, xml_bytes, "");
// Create file attachment (asset) from PVF file
int xml_asset = p.load_asset("Attachment", pvf_name,
"mimetype=text/xml description={ZUGFeRD invoice in XML format} "
+ "relationship=Alternative documentattachment=true");
// Associate file attachment with the document
p.end_document("associatedfiles={" + xml_asset + "}");
So I thought, take the example and fit it to perl:
my $xmldata = read_file($xmlfile, binmode => ':utf8'); #I use example xml at the moment
my $pvf_xml = "/pvf/ZUGFeRD-invoice.xml";
PDF_create_pvf($pdf, $pvf_xml, $xmldata, ""); #because no OOP i need to call it this way (works with all other PDF Functions)
my $xml_invoice = PDF_load_asset("Attachment", $pvf_xml, "mimetype=text/xml "
."description={Rechnungsdaten im Zugferd-Xml-Format} "
."relationship=Alternative documentattachment=true");
PDF_end_document($pdf, "associatedfiles={".$xml_invoice."}");
In PHP examples it's also not needed to convert to ByteArray after reading xml. Further tried it with unpack but don't seem to be the problem.
If I call my script I'm just getting:
Usage: load_asset(type, filename, optlist); at signatur_test.pl line
41.
I think the problem is that pvf_xml isn't created the line before.
Anyone did this before and no how to solve this?
Arg, i was just missing the PDF-Handle in the load_asset method:
my $xml_invoice = PDF_load_asset($pdf, "Attachment", $pvf_xml, "mimetype=text/xml "
."description={Rechnungsdaten im Zugferd-Xml-Format} "
."relationship=Alternative documentattachment=true");
This way it works.

Download PDF from URL not ending on pdf generating corrupted PDF file

I am downloading PDF files from URLs using scala using below code and it is working fine
var out: OutputStream = null;
var in: InputStream = null;
val url = new URL( """http://www.pdf995.com/samples/pdf.pdf""")
val connection = url.openConnection().asInstanceOf[HttpURLConnection]
connection.setRequestMethod("GET")
in = connection.getInputStream
val localfile = "sample2.pdf"
out = new BufferedOutputStream(new FileOutputStream(localfile))
val byteArray = Stream.continually(in.read).takeWhile(-1 !=).map(_.toByte).toArray
out.write(byteArray)
but when I give URLs which does not end with "PDF" for example URL given below
https://www.google.com.pk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=51&ved=0ahUKEwjq19ah8MbKAhXEj44KHeWAB6g4MhAWCBgwAA&url=http%3A%2F%2Fwww.us.fulbrightonline.org%2Fuploads%2Ffiles%2Fapplication_samples%2FForm9B_ETA_Reference_Form-Sample.pdf&usg=AFQjCNGZnon3ygHDJnW12Te8JrBR-o6jyw&sig2=OgSgD4HnUXZ9l_VS0AwGFg&bvm=bv.112454388,d.c2E&cad=rja
it does not generate PDF file properly. While opening that PDF "Not a PDF or corrupted error" comes.
If you read your URL and chop off the hash at the end (all that comes after .pdf, you'll see the link that Google is pointing to embedded in there:
https://www.google.com.pk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=51&ved=0ahUKEwjq19ah8MbKAhXEj44KHeWAB6g4MhAWCBgwAA&url=http%3A%2F%2Fwww.us.fulbrightonline.org%2Fuploads%2Ffiles%2Fapplication_samples%2FForm9B_ETA_Reference_Form-Sample.pdf
Here's the direct link (use this for your project):
http://www.us.fulbrightonline.org/uploads/files/application_samples/Form9B_ETA_Reference_Form-Sample.pdf