Background images missing when converting html to pdf with Aspose.HTML

Hello. I like to convert static html to pdf. Eventually in Dokcer but for now I’m trying without Docker. I understood from your response to my ticket that Aspose.HTML is a good package to use.
When I convert html that has an img tag with an external image file, that works wel.
However now I convert a html that has background images, for example
background-image: url('./Images/StapelGeld.png'),
But the background image is missing in the pdf.
I create a HTMLDocument like so:
using var document = new HTMLDocument(html, fullHtmlFilename);
where the 2nd parameter is the full filename of the html file

When converting HTML to PDF using Aspose.HTML, if you notice that background images are missing, it could be due to the way the image paths are specified or how the conversion process is set up. Here are some steps you can take to troubleshoot and resolve the issue:

  1. Check Image Path: Ensure that the path to the background image is correct. If you are using a relative path like url('./Images/StapelGeld.png'), make sure that the base URI is set correctly when creating the HTMLDocument. The base URI should point to the directory where the HTML file and the images are located.

  2. Use Absolute Paths: If possible, try using absolute paths for the background images to see if that resolves the issue. This can help determine if the problem is related to path resolution.

  3. Set PdfSaveOptions: When converting the HTML to PDF, you can use PdfSaveOptions to customize the conversion process. Make sure to specify any necessary options that might affect how resources are loaded.

  4. Sample Code: Here’s a basic example of how to set up the conversion with PdfSaveOptions:

    using Aspose.Html;
    using Aspose.Html.Saving;
    
    // Load the HTML document
    using var document = new HTMLDocument("path/to/your.html", "base/uri/");
    
    // Create PdfSaveOptions
    var options = new PdfSaveOptions();
    
    // Specify any options if needed
    // options.PageSize = new Size(595, 842); // A4 size, for example
    
    // Save the document as PDF
    document.Save("output.pdf", options);
    
  5. Check for Errors: If the issue persists, check for any errors or warnings during the conversion process that might indicate why the background images are not being rendered.

If you continue to experience issues, consider sharing a sample HTML file and the code you are using for further assistance. This will help in diagnosing the problem more effectively.

For more detailed information on converting HTML to PDF, you can refer to the official documentation here.

Sources:
[1]: Convert HTML to PDF Using Java - Aspose Documentation

I tried these things but no result. Probably this is a special case because it is not just an img tag, but
background-image: url('./StapelGeld.png')
in the CSS. Do you other ideas? I also tried
options.CustomLoaderOfExternalResources
but that callback was not called. Probably because I don’t use an external css.

My issue looks a lot like this issue: Background Image CSS Property - #45 by codewarior
But that issue was fixed in Aspose.PDF for .NET 11.6.0. But I’m using Aspose.PDF 24.7.0.

I’m having the same issue, see Background images missing when converting html to pdf with Aspose.HTML - #4 by srorije
But I use Aspose.PDF 24.7.0. Can it be that this isse has re-emerged?

@srorije

Would you please share your sample HTML in .zip format with us so that we can test the scenario in our environment and address it accordingly?

Hello,
I have attached a zip with the source code, including the html. There are two endpoints. The first converts html/index.html to pdf. It contains a normal image using the img tag. This works fine. The second endpoint converts html2/Loonstroken2.html. This html contains 3 background images. These images do not appear in the pdf. Note that when I construct the Document object, I pass HtmlLoadOptions in which the basepath is set to a certain value.
Kind regards,
Sander Rorije
WebAppMetHtmlFragment.zip (164.7 KB)

@srorije

Seems like you are using Aspose.PDF and issue is happening using it. We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-57866

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.