using itextpdf for converting a html to pdf. There are several images to be converted. However, sometimes image addresses are not correct. In such cases, I get a "file is not a recognized image format" error and the conversion stops.
I catch the exception but I don't know how to continue conversion bypassing that image.
Any ideas ?
my code :
try {
Document document = new Document( com.itextpdf.text.PageSize.A4 );
String fileNameWithPath = filename;
FileOutputStream fos = new FileOutputStream( fileNameWithPath );
PdfWriter pdfWriter = PdfWriter.getInstance( document, fos );
document.open();
document.addCreationDate();
);
HTMLWorker htmlWorker = new HTMLWorker( document );
htmlWorker.parse( new StringReader( htmlText ) );
document.close();
fos.close();
return true;
}
catch(DocumentException e) {
e.printStackTrace();
return false;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}catch (Exception e) {
File file = new File(absoluteFilePath);
if(file.exists()) {
boolean isDeleted = file.delete();
Log.i(TAG, "PDF isDeleted: " + isDeleted);
}
Log.d(TAG, "Exception: " + e.getMessage());
e.printStackTrace();
return false;
}
}
and the error I get :
03-27 22:54:27.065: D/rerore(27830): Exception: file:/ is not a recognized imageformat. 03-27 22:54:27.065: W/System.err(27830): ExceptionConverter: java.io.IOException: file:/ is not a recognized imageformat. 03-27 22:54:27.077: W/System.err(27830): at com.itextpdf.text.Image.getInstance(Image.java:318) 03-27 22:54:27.077: W/System.err(27830): at com.itextpdf.text.Image.getInstance(Image.java:340) 03-27 22:54:27.077: W/System.err(27830): at com.itextpdf.text.html.simpleparser.ElementFactory.createImage(ElementFactory.java:425) 03-27 22:54:27.077: W/System.err(27830): at com.itextpdf.text.html.simpleparser.HTMLWorker.createImage(HTMLWorker.java:454) 03-27 22:54:27.077: W/System.err(27830): at com.itextpdf.text.html.simpleparser.HTMLTagProcessors$14.startElement(HTMLTagProcessors.java:431) 03-27 22:54:27.077: W/System.err(27830): at com.itextpdf.text.html.simpleparser.HTMLWorker.startElement(HTMLWorker.java:193) 03-27 22:54:27.077: W/System.err(27830): at com.itextpdf.text.xml.simpleparser.SimpleXMLParser.processTag(SimpleXMLParser.java:581) 03-27 22:54:27.077: W/System.err(27830): at com.itextpdf.text.xml.simpleparser.SimpleXMLParser.go(SimpleXMLParser.java:323) 03-27 22:54:27.077: W/System.err(27830): at com.itextpdf.text.xml.simpleparser.SimpleXMLParser.parse(SimpleXMLParser.java:607) 03-27 22:54:27.077: W/System.err(27830): at com.itextpdf.text.html.simpleparser.HTMLWorker.parse(HTMLWorker.java:147) 03-27 22:54:27.077: W/System.err(27830): at com.zagabun.yyy.MainActivity.createpdf(MainActivity.java:1039) 03-27 22:54:27.077: W/System.err(27830): at com.zagabun.yyy.MainActivity$6.run(MainActivity.java:974)
this is not a duplicate question because I've reviewed my question and deleted the old one.
also , now I have a solution for invalid images. I check the URL of each image before running the HTMLWorker parse command. However, there are other errors with other tags and I am not sure how error handling is done with iText 5.
can anybody help ?