Background image not visible on converting html to pdf

We are trying to convert html to pdf using aspose words in java. The background image is not not visible in the converted pdf.
We are using aspose words 19.12 version.

@Rashi_Bansal

Could you please ZIP and attach your input HTML, problematic and expected output PDF files here for testing? We will investigate this issue and provide you more information on it.

aspose.zip (14.1 KB)
Hello Tahir,
I have attached the html that we want to convert to pdf and the converted pdf. In the output pdf , the background image is expected which can be seen in the html.
Thanks.

@Rashi_Bansal

Please note that Aspose.Words mimics the behavior of MS Word. If you perform the same scenario using MS Word, you will get the same output.

So do we have any work around for this?
Actually we want to convert this html to image and initially we used aspose html to convert the html to image directly. But it gave null pointer exception in centOS and it took a lot of time in windows.
So as a work around, we converted the html to pdf using aspose words and then pdf to image using aspose pdf.
This took lesser time on windows in comparison to aspose html and aslo worked in centOS.

So please provide a solution to convert this html to image where we can see the background as well using any aspose java library which works on both windows and centOS.

For your reference, following is the error which we got using aspose html to convert html to image in centOS :
java.lang.NullPointerException
at com.aspose.html.internal.p226.z1.m1(Unknown Source)
at com.aspose.html.internal.p226.z1.m1(Unknown Source)
at com.aspose.html.internal.p202.z12.initialize(Unknown Source)
at com.aspose.html.internal.p202.z4.m3415(Unknown Source)
at com.aspose.html.internal.p202.z15.m2958(Unknown Source)
at com.aspose.html.internal.p202.z15.m1(Unknown Source)
at com.aspose.html.internal.p202.z15.m1(Unknown Source)
at com.aspose.html.internal.p202.z10.m1(Unknown Source)
at com.aspose.html.internal.p202.z10.m1(Unknown Source)
at com.aspose.html.internal.p202.z27.m1(Unknown Source)
at com.aspose.html.internal.p202.z27.m1(Unknown Source)
at com.aspose.html.internal.p202.z27.m1(Unknown Source)
at com.aspose.html.internal.p202.z27.m1(Unknown Source)
at com.aspose.html.internal.p202.z59.m1(Unknown Source)
at com.aspose.html.internal.p202.z2.m1(Unknown Source)
at com.aspose.html.internal.p202.z59.m1(Unknown Source)
at com.aspose.html.internal.p202.z59.m3(Unknown Source)
at com.aspose.html.internal.p202.z2.m1(Unknown Source)
at com.aspose.html.internal.p202.z59.m1(Unknown Source)
at com.aspose.html.internal.p202.z62.m3426(Unknown Source)
at com.aspose.html.internal.p153.z5.m2(Unknown Source)
at com.aspose.html.internal.p173.z10.m2868(Unknown Source)
at com.aspose.html.internal.p166.z2.m2(Unknown Source)
at com.aspose.html.internal.p166.z2.m2(Unknown Source)
at com.aspose.html.internal.p166.z2.m1(Unknown Source)
at com.aspose.html.internal.p151.z3.m1(Unknown Source)
at com.aspose.html.internal.p152.z1.m1(Unknown Source)
at com.aspose.html.internal.p257.z1.m1(Unknown Source)
at com.aspose.html.internal.p161.z4.m1(Unknown Source)
at com.aspose.html.internal.p161.z2.m5(Unknown Source)
at com.aspose.html.collections.z3.m126(Unknown Source)
at com.aspose.html.collections.z3.hasNext(Unknown Source)
at com.aspose.html.internal.p257.z1.m1(Unknown Source)
at com.aspose.html.rendering.HtmlRenderer.render(Unknown Source)
at com.aspose.html.rendering.HtmlRenderer.render(Unknown Source)
at com.aspose.html.rendering.Renderer.render(Unknown Source)
at com.aspose.html.rendering.Renderer.render(Unknown Source)
at com.aspose.html.converters.z5.m1(Unknown Source)
at com.aspose.html.converters.z5.m1(Unknown Source)
at com.aspose.html.converters.z5.m1(Unknown Source)
at com.aspose.html.converters.Converter.convertHTML(Unknown Source)

@Rashi_Bansal

You are using Aspose.HTML to convert HTML to PDF. So, we are moving this forum thread to respective forum where you will be guided appropriately.

@Rashi_Bansal

Would you kindly share the sample code snippet that you used to convert the HTML into Image? We will test the scenario in our environment and address it accordingly.

Hello Asad,
aspose (2).zip (6.0 KB)
Please find the attached zip. this contains the code snippet and the sample html.
We are trying to convert html to jpg. It is working fine in windows but it gives null pointer exception in centos (even a basic html containing h1 tag gives the error).
Even in the windows, the conversion is taking a lot of time.

PS: This is not replated to background image. We were facing the background image issue when we were using aspose words as a workaround.

@Rashi_Bansal

We need to further investigate this case in details. For the purpose, an investigation ticket as HTMLJAVA-1157 has been logged in our issue tracking system. We will further look into its details and keep you posted with the status of its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.

Hello Asad
Can you please update me on this issue ?

@Rashi_Bansal

We are afraid that the earlier logged ticket has not been yet resolved. We will surely fix it on a first come first serve basis and let you know as soon as it is resolved. Please spare us some time.

We are sorry for the inconvenience.

Thanks for the update. Can you please share some rough timeline by when we can get this fixed so that we can plan this fix in our product release.

@Rashi_Bansal

We really regret that we cannot share any reliable or rough ETA at the moment as the ticket has not been fully investigated yet. Please note that the issues in free support model are resolved on first come first serve basis and their resolution time depends upon the number of issues in the queue. Nevertheless, we have recorded your concerns and will surely inform you once we have some update about ticket resolution or ETA. We highly appreciate your patience in this regard.

We apologize for the inconvenience caused.

Any updates on this?

@Rashi_Bansal

We are afraid that the earlier logged ticket could not get resolved yet. However, we have recorded your concerns and will definitely inform you in this forum thread as soon as we have some definite updates regarding ticket resolution. Please spare us some time.

We apologize for the inconvenience.

Hi,

Has this been fixed yet? I am getting the same error (slightly different call stack) when converting from HTML to PDF:

	public static byte[] convertHtmlToPdf(InputStream is, final String baseUrl) {
	HtmlLoadOptions options = new HtmlLoadOptions();
    options.setCustomLoaderOfExternalResources(new LoadOptions.ResourceLoadingStrategy() {
        public LoadOptions.ResourceLoadingResult invoke(String resourceURI) {
    		log.info("Resource URI is {} ", resourceURI);
            if (resourceURI.startsWith("file:///")) {
            	String path = baseUrl + resourceURI.substring(7);
        		log.info("Resource path is {} ", path);
            	try {
					byte[] bytes = getFileBytes(path);
					int len = bytes.length;
            		log.info("Resource path {} is {} bytes long", path, len);
            		if (len == 0) {
                        LoadOptions.ResourceLoadingResult res = new LoadOptions.ResourceLoadingResult(new byte[] {});
                        res.setLoadingCancelled(true);
                        return(res);
            		}
	                LoadOptions.ResourceLoadingResult res = new LoadOptions.ResourceLoadingResult(bytes);
	                return(res);
				}
            	catch (IOException e) {
            		log.warn("Resource URI loading of {} failed:", path, e);
				}
            }
            LoadOptions.ResourceLoadingResult res = new LoadOptions.ResourceLoadingResult(new byte[] {});
            res.setLoadingCancelled(true);
            return(res);
        }
    });

	Document document = new Document(is, options);
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	document.save(baos);
	byte[] bytes = baos.toByteArray();
	return(bytes);
}

The variable ‘is’ is the bytes of a String of HTML I get:

SEVERE: 1: Error
java.lang.NullPointerException
at com.aspose.html.internal.p226.z1.m1(Unknown Source)
at com.aspose.html.internal.p226.z1.m1(Unknown Source)
at com.aspose.html.internal.p202.z12.initialize(Unknown Source)
at com.aspose.html.internal.p202.z4.m3416(Unknown Source)
at com.aspose.html.internal.p202.z15.m2959(Unknown Source)
at com.aspose.html.internal.p202.z15.m1(Unknown Source)
at com.aspose.html.internal.p202.z15.m1(Unknown Source)
at com.aspose.html.internal.p202.z10.m1(Unknown Source)
at com.aspose.html.internal.p202.z10.m1(Unknown Source)
at com.aspose.html.internal.p202.z27.m1(Unknown Source)
at com.aspose.html.internal.p202.z27.m1(Unknown Source)
at com.aspose.html.internal.p202.z27.m1(Unknown Source)
at com.aspose.html.internal.p202.z27.m1(Unknown Source)
at com.aspose.html.internal.p202.z59.m1(Unknown Source)
at com.aspose.html.internal.p202.z2.m1(Unknown Source)
at com.aspose.html.internal.p202.z59.m1(Unknown Source)
at com.aspose.html.internal.p202.z59.m3(Unknown Source)
at com.aspose.html.internal.p202.z2.m1(Unknown Source)
at com.aspose.html.internal.p202.z59.m1(Unknown Source)
at com.aspose.html.internal.p202.z62.m3427(Unknown Source)
at com.aspose.html.internal.p153.z5.m2(Unknown Source)
at com.aspose.html.internal.p173.z10.m2869(Unknown Source)
at com.aspose.html.internal.p166.z2.m2(Unknown Source)
at com.aspose.html.internal.p166.z2.m2(Unknown Source)
at com.aspose.html.internal.p166.z2.m1(Unknown Source)
at com.aspose.html.internal.p151.z3.m1(Unknown Source)
at com.aspose.html.internal.p152.z1.m1(Unknown Source)
at com.aspose.html.internal.p257.z1.m1(Unknown Source)
at com.aspose.html.internal.p161.z4.m1(Unknown Source)
at com.aspose.html.internal.p161.z2.m5(Unknown Source)
at com.aspose.html.collections.z3.m127(Unknown Source)
at com.aspose.html.collections.z3.hasNext(Unknown Source)
at com.aspose.html.internal.p257.z1.m1(Unknown Source)
at com.aspose.html.rendering.HtmlRenderer.render(Unknown Source)
at com.aspose.html.rendering.HtmlRenderer.render(Unknown Source)
at com.aspose.html.rendering.Renderer.render(Unknown Source)
at com.aspose.html.rendering.Renderer.render(Unknown Source)
at com.aspose.html.converters.z5.m1(Unknown Source)
at com.aspose.html.converters.z5.m1(Unknown Source)
at com.aspose.html.converters.Converter.convertHTML(Unknown Source)
at com.aspose.html.converters.Converter.convertHTML(Unknown Source)
at com.pacccc.asposeutils.HTMLConversion.convert(HTMLConversion.java:54)
at uk.co.bespokemvn17.lannn.DocumentExtract.convertGUIFormToPDF(DocumentExtract.java:380)
at uk.co.bespokemvn17.lannn.DocumentExtract.processCase(DocumentExtract.java:175)
at uk.co.bespokemvn17.lannn.DocumentExtract.documentExtract(DocumentExtract.java:134)

I am using version 22.4 (because in 22.5 there seems to be a change in the API around SaveFormat). I am using Java 1.7. On Ubuntu 20.04 it works fine. My problem is when running on IBM iSeries OS/400 v 7.3 (also Java 1.7). There seems to be several different ways in Aspose of converting HTML to PDF. I have tried a few now and they all work (not all load the resources using a base URL starting with file://) but none so far work on iSeries, all getting some kind of NPE. Any clue as to what is going wrong would be helpful as it might point to a work around. Maybe something to do with fonts?? Hopefully the Aspose code is pure Java - please let me know if not.

Thanks Mark.

@mawheadon

The linked ticket was logged for the CentOS operating system and regretfully, it could not get fully investigated yet. However, missing fonts can also be a cause of the error in your case. Please make sure that all MS Essential Fonts are present and properly installed in the system where you are working. In case issue still persists, please share sample HTML file with us so that we can further log an investigation ticket and share the ID with you.

@asad.ali Thanks for that. I am under the impression that I need to call com.aspose.pdf.FontRepository.addLocalFontPath() to point to the truetype fonts, is that right?

@mawheadon

Yes, in Aspose.PDF for Java, you can use the mentioned method to set the font directory according to your system so that the API will have access to the available fonts and use them while generating the document accordingly.