Pdf not generated using iText PDF when image not found - java

Code is working well when image is found on said path.
Code:
...
Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream("D:/pdfSample.pdf"));
document.open();
String text="<html><body>hello<img src='http://pathOfImage/Image.gif' alt='' /></body></html>";
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(text));
document.close();
System.out.println( "PDF Created!" );
But if image is not found, pdf is not getting generated i.e. image alt property is not working. What can be done?

Related

Svg Image is not properly render in the pdf

[]
1[]2]3
Here is my code to add svg image in the pdf using itext 7.1.3 and also using SVGConventer.The image not properly rendered, i am not able understand where and what is the problem? Can anyone help me. Thanks in advance.
PdfWriter writer = new PdfWriter(new FileOutputStream("/home/users/Documents/pdf/new.pdf"));
PdfDocument pdfDoc = new PdfDocument(writer);
Document doc = new Document(pdfDoc);
URL svgUrl = new File(svg).toURI().toURL();
doc.add(new Paragraph("new pikachu"));
Image image = SvgConverter.convertToImage(svgUrl.openStream(), pdfDoc);
doc.add(image);
doc.close();

Conversion of HTML( with inline css) to PDF using itext 2.1.7

I want to convert one html page to pdf using itext 2.1.7. I have used HTMLWorker to convert the html file, but it not taking the inline css which I have used in the html. Below is my code snippet . Can anyone help to fix this issue..
PdfWriter pdfWriter = PdfWriter.getInstance(document, new
FileOutputStream("D:/testpdf.pdf"));
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(htmlContent));
document.close();
Thanks in Advance !
Use itext7-7.0.2 because iText 2.1.7 didn't support inline CSS.
String htmlContent = "<html><body style='color:red'> PDF project </body></html>";
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(new File("C:\\testpdf.pdf")));
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(htmlContent));
document.close();

how to solve read only file system error in android programming with eclipse

I am developing a program in which i must create pdf file inside application.
This is the code that i use for creating pdf file but an error occurred that say
"/Image.pdf:open failed:EROFS(read-only file system)"
This is my button click code:
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream("Image.pdf"));
document.open();
Image image1 = Image.getInstance("watermark.png");
document.add(image1);
String imageUrl = "http://jenkov.com/images/20081123-20081123-3E1W7902-small-portrait.jpg";
Image image2 = Image.getInstance(new URL(imageUrl));
document.add(image2);
document.close();
Try to write pdf in sdcard root directory:
PdfWriter.getInstance(document,new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"Image.pdf"));
first i thank Haresh Chhelana for his answers.
by using this code i can create new pdf file with an image in it.
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Image.pdf"));
document.open();
Image image1 = Image.getInstance(Environment.getExternalStorageDirectory()+"/01.jpg");
document.add(image1);
document.close();

pdfwriter doesn't translate special characters

I have HTML file with an external CSS. I want to create PDF from the HTML file, but the endcoing doesn't work. HTML file works fine, but after transfering to PDF, some characters in PDF are missing. (čřě...) It happens even if I set the Charset in PDFWriter constructor.
How do I solve this, please?
public void createPDF() {
try {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(username + ID + ".pdf"));
document.open();
String hovinko = username + ID + ".html";
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(hovinko), Charset.forName("UTF-8"));
document.close();
System.out.println("PDF Created!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
Did you try to convert your special characters before writing them to your PDF?
yourHTMLString.replaceAll(oldChar, newChar);
ć = ć
ř = ř
ě = ě
If you need more special characters, visit this link.
EDIT: Then try this out, it worked for me:
BaseFont basefont = BaseFont.createFont("C:/Windows/Fonts/ARIAL.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(basefont, 12);
document.add(new Paragraph("čřě", font));
Try it with below logic. It worked for me:
InputStream is = new ByteArrayInputStream(hovinko.getBytes(Charset.forName("UTF-8")));
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is, Charset.forName("UTF-8"));
I used xmlworker version 5.5.12 and itextpdf version 5.5.12.
I was strugling with sam problem (Polish special signs).
For me solution was to write a good font-family in html code.

iText style parsing HTML to PDF

I've a problem with iText.
I've followed this link: How to export html page to pdf format?
My snippet:
String str = "<html><head><body><div style=\"width:100%;height:100%;\"><h3 style=\"margin-left:5px;margin-top:40px\">First</h3><div style=\"margin-left:15px;margin-top:15px\"><title></title><p>sdasdasd shshshshdffgdfgd</p></div><h3 style=\"margin-left:5px;margin-top:40px\">The dream</h3><div style=\"margin-left:15px;margin-top:15px\"></div></div></body></head></html>";
String fileNameWithPath = "/Users/cecco/Desktop/pdf2.pdf";
com.itextpdf.text.Document document =
new com.itextpdf.text.Document(com.itextpdf.text.PageSize.A4);
FileOutputStream fos = new FileOutputStream(fileNameWithPath);
com.itextpdf.text.pdf.PdfWriter pdfWriter =
com.itextpdf.text.pdf.PdfWriter.getInstance(document, fos);
document.open();
document.addAuthor("Myself");
document.addSubject("My Subject");
document.addCreationDate();
document.addTitle("My Title");
com.itextpdf.text.html.simpleparser.HTMLWorker htmlWorker =
new com.itextpdf.text.html.simpleparser.HTMLWorker(document);
htmlWorker.parse(new StringReader(str.toString()));
document.close();
fos.close();
and work fine.
But tag style into h3 and div aren't considered.
But if I copy my html into http://htmledit.squarefree.com/ all is correct.
How can I solve this problem?
iText isn't the best Html Parser, but you can use Flying-Saucer for this. Flying-Saucer is build on top of iText but has a capable Xml / (X)Html parser. Short: Flying Saucer is perfect if you want html -> Pdf.
Here's how to generate the pdf from your string:
/*
* Note: i filled something in the title-tag and fixed the head tag (the whole body-tag was in the head)
*/
String str = "<html><head></head><body><div style=\"width:100%;height:100%;\"><h3 style=\"margin-left:5px;margin-top:40px\">First</h3><div style=\"margin-left:15px;margin-top:15px\"><title>t</title><p>sdasdasd shshshshdffgdfgd</p></div><h3 style=\"margin-left:5px;margin-top:40px\">The dream</h3><div style=\"margin-left:15px;margin-top:15px\"></div></div></body></html>";
OutputStream os = new FileOutputStream(new File("example.pdf"));
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(str);
renderer.layout();
renderer.createPDF(os);
os.close();
But: FS supports only valid Html / Xhtml / xml, so make shure it is.

Categories

Resources