Hello,
I cant seem to convert a particular page for html to a pdf. I just keep getting an error stating that the width or height cannot be 0. The html file renders fine on its out. I should note that other html files work just fine the only difference is that this one is to be rendered in portrait.
HtmlLoadOptions options = new HtmlLoadOptions
{
PageInfo = {Width = 816,
Height = 1056 },
// set Print or Screen mode
HtmlMediaType = HtmlMediaType.Print,
//IsEmbedFonts = true.
IsEmbedFonts = false
};
options.PageInfo.Margin.Left = 0;
options.PageInfo.Margin.Right = 0;
options.PageInfo.Margin.Top = 0;
options.PageInfo.Margin.Bottom = 0;
options.PageLayoutOption = HtmlPageLayoutOption.ScaleToPageWidth;
Document pdfDocument= new Document(inputHtmlFilename, options);
@kfadel97
The error you are encountering, which states that the width or height cannot be 0, typically occurs when the dimensions specified for the HTML content are not valid. In your case, you are setting the page dimensions in the HtmlLoadOptions
, but it seems that the HTML content might not be rendering correctly due to the specified dimensions or margins.
Here are a few suggestions to troubleshoot and resolve the issue:
-
Check HTML Content: Ensure that the HTML content you are trying to convert is valid and does not contain any elements that might cause rendering issues. You can test the HTML in a web browser to confirm it displays correctly.
-
Adjust Page Dimensions: You are currently setting the page dimensions to 816x1056. Make sure these dimensions are appropriate for the content you are trying to convert. You might want to try different dimensions or even remove the width and height settings temporarily to see if the conversion works without them.
-
Margins: You have set all margins to 0. While this is valid, it might lead to issues if the content requires some space. Try setting some default margins (e.g., 10 or 20) to see if that resolves the issue.
-
HtmlMediaType: You are using HtmlMediaType.Print
. If your HTML is designed for screen display, consider changing this to HtmlMediaType.Screen
to see if it affects the rendering.
-
Debugging: You can add exception handling to capture more details about the error. This might provide additional insights into what is going wrong during the conversion process.
Here’s a modified version of your code with some of these suggestions:
HtmlLoadOptions options = new HtmlLoadOptions
{
PageInfo = { Width = 816, Height = 1056 },
HtmlMediaType = HtmlMediaType.Screen, // Change to Screen if applicable
IsEmbedFonts = false
};
// Set some default margins
options.PageInfo.Margin.Left = 10;
options.PageInfo.Margin.Right = 10;
options.PageInfo.Margin.Top = 10;
options.PageInfo.Margin.Bottom = 10;
options.PageLayoutOption = HtmlPageLayoutOption.ScaleToPageWidth;
try
{
Document pdfDocument = new Document(inputHtmlFilename, options);
// Save the PDF document
}
catch (Exception ex)
{
Console.WriteLine($"Error during conversion: {ex.Message}");
}
By following these steps, you should be able to identify the cause of the error and successfully convert your HTML to PDF. If the problem persists, consider checking the Aspose documentation or forums for more specific guidance related to your HTML content.
@kfadel97
Could you provide the file you mentioned?
I cant seem to link the html file. I can post the markup in a .docx file if that works for you?
I have tried all of this and none of it seems to have effected the outcome. The html seems to appear normal in the browser so I am not sure what is breaking it.
Here is the exact error.
System.ArgumentException: ‘Rectangle ‘{X=46.373,Y=162.425,Width=166.19,Height=0}’ cannot have a width or height equal to 0.’
I am not sure what the dimensions there are refering to. If its to the total dimensions of the html then something is wrong here.
@kfadel97
You can attach it as zip archive, it usually works
vphtml.zip (4.9 KB)
I should note this was converted initially from a pdf to this html from aspose. So far this is the only screen that has not been able to reconvert.
@kfadel97
Do I understand correctly that you converted pdf to HTML and then tried to convert it back?
If that’s the case, could you also provide version of Aspose PDF you are using?
24.6 is the version used
Edit: The issue seems to be with the embedded svg but I am not sure what graphic is broken within it.
Edit 2: It is with the linear gradiant graphic id is 1125 for it. This was generated from the aspose process so I am not sure why it cant be reprocessed.
@kfadel97
I’ll investigate this issue and check if it’s possible to resolve it with current version
@kfadel97
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-58121
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.
Currently I found no workaround so I added task for development team as it appears as bug. I’ll try to contact developers, maybe they’ll suggest some temporary solution
If there will be any news I’ll contact you as soon as possible.
Thank you for looking into it. As a workaround currently I am just replacing that graphic.
1 Like
The issues you have found earlier (filed as PDFNET-58121) have been fixed in Aspose.PDF for .NET 24.10.