HTML with embedded CSS to PDF

Hello.


I am using Aspose.pdf for JAVA. Tried with licensed aspose versions( aspose-pdf-9.7.1-jdk16, aspose-pdf-10.3.0), java version 1.7.

Pdf converted doesn’t look close to Html. It took 12 seconds for the conversion, does it always take that long.

Attached zip file contains, java code, template file and generated pdf output.

It would be great, if you could take a quick look at it.

Thanks
Martin

Hi Martin,


Thanks for your inquiry. I have tested your shared HTML file and noticed that text is being moving off the page in resultant PDF. So logged a ticket PDFNEWJAVA-34893 for further investigation and resolution.

Moreover about time, It does not take as long for every file, it depends upon the contents/size of file and system resources.

We are sorry for the inconvenience caused.

Best Regards,

Thanks for taking a look at it.


I figured it out, pdf generation doesn’t work at all, if the css has “float” attribute on it. Images also doesn’t show up on the generated pdf, if tag has width attribute on it.

Martin

One another quick info…

pdf generated with an html file looks different than the pdf generated with an injected html content.

is it always the case?

// HTML template file
om.aspose.pdf.Document doc = new com.aspose.pdf.Document(base.concat("\template\").concat(htmlTemplate).concat(".html"), htmlOptions);
doc.save(base.concat("\out\") + “HelloWorld_html” + “" + String.valueOf(System.currentTimeMillis()) +".pdf”);

// HTML content injected
Pdf pdfDoc = new Pdf();
Section sec = pdfDoc.getSections().add();
Text docContentinHTML = new Text(htmlFeedDoc.html());
docContentinHTML.isHtmlTagSupported(true);
sec.getParagraphs().add(docContentinHTML);
String out = base.concat("\out\").concat(String.valueOf(docId)).concat(".pdf");
pdfDoc.save(out);

is there any other way to inject html code?

Thanks in advance.
Hi Martin,

manthonisamy:
Thanks for taking a look at it.

I figured it out, pdf generation doesn't work at all, if the css has "float" attribute on it. Images also doesn't show up on the generated pdf, if tag has width attribute on it.

Martin

Thanks for sharing your findings. We have passed it to our product team, they will consider while investigating the issue. We will notify you once logged issue is resolved.

Best Regards,

Hi Martin,

manthonisamy:
One another quick info...
pdf generated with an html file looks different than the pdf generated with an injected html content.

is it always the case?

// HTML template file
om.aspose.pdf.Document doc = new com.aspose.pdf.Document(base.concat("\\template\\").concat(htmlTemplate).concat(".html"), htmlOptions);
doc.save(base.concat("\\out\\") + "HelloWorld_html" + "_" + String.valueOf(System.currentTimeMillis()) +"_.pdf");

// HTML content injected
Pdf pdfDoc = new Pdf();
Section sec = pdfDoc.getSections().add();
Text docContentinHTML = new Text(htmlFeedDoc.html());
docContentinHTML.isHtmlTagSupported(true);
sec.getParagraphs().add(docContentinHTML);
String out = base.concat("\\out\\").concat(String.valueOf(docId)).concat(".pdf");
pdfDoc.save(out);

is there any other way to inject html code?

Thanks in advance.

It is recommend to you use new generator (com.aspose.pdf). Old generator(aspose.pdf) will be obsolete in near future. You may add HTML string to PDF using new generator. Please check following documentation link for the purpose. It will help you to accomplish the task.


Please feel free to contact us for any further assistance.

Best Regards,

@manthonisamy

Thanks for your patience.

We are pleased to inform you that earlier logged issue PDFJAVA-34893 has been resolved in Aspose.PDF for Java 18.1. Please download latest version of the API and in case of any issue, feel free to contact us.