I am attempting to convert the following HTML to PDF using Aspose.PDF for Java v24.8…
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<style>
body {
line-height: 1;
font-family: "Times New Roman", serif;
font-size: 11pt;
margin: 0;
padding: 0;
}
p {
margin: 0 0 6pt 0;
padding: 0;
}
figure {
padding: 0;
margin: 0;
}
.table table {
border-collapse: collapse;
border-spacing: 0;
width: 100%;
height: 100%;
}
.ck-table-resized {
table-layout: fixed;
}
figure.image {
display: table;
clear: both;
text-align: center;
margin: .9em auto;
min-width: 50px;
}
figure.image img {
display: block;
margin: 0 auto;
max-width: 100%;
min-width: 100%;
height: auto;
}
figure.image.image-style-align-center {
margin-left: auto;
margin-right: auto;
}
figure.image.image-style-align-left {
float: left;
clear: none;
}
figure.image.image-style-align-right {
float: right;
clear: none;
}
figure.image.image-style-block-align-left {
margin-left: 0;
margin-right: auto;
}
figure.image.image-style-block-align-right {
margin-right: 0;
margin-left: auto;
}
</style>
</head>
<body>
<p>Red dot...</p><figure class="image image-style-block-align-left"><img style="aspect-ratio:5/5;" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" width="5" height="5"></figure><p> </p></body>
</html>
Aspose.PDF hangs while reading the HTML document with this code…
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(inputStream, new com.aspose.pdf.HtmlLoadOptions());
Reading HTML without an encoded img works ok.
@rgabbard
It seems that you are experiencing an issue where Aspose.PDF hangs during the conversion of HTML to PDF when using a base64 encoded image. This can sometimes occur due to the complexity of the HTML content or the way images are handled during the conversion process.
Here are a few suggestions to troubleshoot and potentially resolve the issue:
-
Update Aspose.PDF Library: Ensure that you are using the latest version of the Aspose.PDF library. Sometimes, bugs are fixed in newer releases, and updating might resolve the hanging issue.
-
Simplify HTML Content: Try simplifying your HTML content to see if the issue persists. For example, remove the base64 image and see if the conversion works without it. If it does, gradually add back elements to identify what might be causing the hang.
-
Use HtmlLoadOptions: When loading your HTML, you can customize the HtmlLoadOptions
. For instance, you can set options to manage how external resources are loaded. This can sometimes help with performance issues:
HtmlLoadOptions options = new HtmlLoadOptions();
options.setCustomLoaderOfExternalResources(new LoadOptions.ResourceLoadingStrategy() {
public LoadOptions.ResourceLoadingResult invoke(String resourceURI) {
// Custom logic for loading resources
return new LoadOptions.ResourceLoadingResult(new byte[]{});
}
});
Document pdfDocument = new Document(inputStream, options);
-
Check Image Size: Ensure that the base64 image is not excessively large. Large images can lead to performance issues during conversion. If possible, try using smaller images or optimizing the image size.
-
Debugging: If the problem persists, consider enabling logging or debugging to capture more information about where the process hangs. This can provide insights into whether it’s a specific part of the HTML or image processing that is causing the issue.
If none of these suggestions resolve the issue, you may want to reach out to Aspose support with specific details about your environment and the HTML content you are trying to convert.
Feel free to ask if you need further assistance!
Sources:
[1]: Convert HTML to PDF Files | Aspose.PDF Java PDF Processing API
Done, done and done. Can a human look at this?
@rgabbard
Are you reading the HTML string as Stream or loading an HTML file? Can you please share how you are initializing the inputStream? Also, please share what OS and environment are you using while using the code?
The HTML is stored as a string and the input stream is constructed like so…
InputStream inputStream = new ByteArrayInputStream(htmlSrc.getBytes());
I’ve encountered the problem when running on MacOS Sonoma 14.6.1. If I send HTML content without the base64 encoded img it works fine.
@rgabbard
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): PDFJAVA-44322
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.