Unable to convert a web-page content to pdf - System.ArgumentException: Incompatible unit types

Hi!
I have the following URL of a web page that I want to convert to PDF: https://www.theverge.com/2023/1/16/23557098/generative-ai-art-copyright-legal-lawsuit-stable-diffusion-midjourney-deviantart/

I also have some code examples (C# .Net) that were taken from here: Convert HTML to PDF in .NET|Aspose.PDF for .NET

Here is my debug code based on it:

    // Example1 - doesn't work: System.ArgumentException: Incompatible unit types
    var link = "https://www.theverge.com/2023/1/16/23557098/generative-ai-art-copyright-legal-lawsuit-stable-diffusion-midjourney-deviantart/";
    var url = new Url($"{link}");
    var pdfSaveOptions = new Aspose.Html.Saving.PdfSaveOptions();
    Converter.ConvertHTML(url, pdfSaveOptions, @"test-url-conversion.pdf"); // error is here


    // Example2 - doesn't work too, same error
    var options = new HtmlLoadOptions(link)
    {
        PageInfo = { Width = 842, Height = 1191, IsLandscape = true }
    };
    var pdfDocument = new Document(GetContentFromUrlAsStream(link), options); // error is here
    pdfDocument.Save("test-stream-conversion.pdf");


    // GetContentFromUrlAsStream - implementation was taken as is
    private Stream GetContentFromUrlAsStream(string url, ICredentials credentials = null)
    {
        using (var handler = new HttpClientHandler { Credentials = credentials })
        using (var httpClient = new HttpClient(handler))
        {
            return httpClient.GetStreamAsync(url).GetAwaiter().GetResult();
        }
    }

However, it doesn’t work. Both examples throw the System.ArgumentException: Incompatible unit types
I’m using the latest version of Aspose.Pdf (23.5.0) and Aspose.Html (23.5.0)
This code above works fine for other websites but doesn’t work for this specific URL.

In addition, there is an interesting detail: I’ve also tried your online service Convert WebPage to PDF and it works fine with my URL! It converts my URL without any error. So, could you please provide more information about the implementation details? How can I make it work from my code in the same way?

@h0las

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-54754

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.

@h0las

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): HTMLNET-4616

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.

@h0las

We tried using below code snippet in our environment with Aspose.PDF for .NET and faced a different exception. The codebase of the free online app is also not uploaded yet in our GitHub repository. Therefore, two different tickets have been generated in our issue tracking system to investigate this case. We will let you know as soon as they are resolved. Please spare us some time.

We are sorry for the inconvenience.

var url = "https://www.theverge.com/2023/1/16/23557098/generative-ai-art-copyright-legal-lawsuit-stable-diffusion-midjourney-deviantart/";

System.Net.WebRequest request = System.Net.WebRequest.Create(url);
// If required by the server, set the credentials.
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Time out in miliseconds before the request times out
request.Timeout = 1000;
// Get the response.
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();

MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(responseFromServer));
HtmlLoadOptions options = new HtmlLoadOptions(url);
options.PageInfo.IsLandscape = false;
options.PageInfo.Margin = new MarginInfo() { Bottom = 0, Left = 0, Right = 0, Top = 0 };
options.PageInfo.Height = Aspose.Pdf.PageSize.PageLetter.Height;
options.PageInfo.Width = Aspose.Pdf.PageSize.PageLetter.Width;
// Load HTML file
Document pdfDocument = new Document(stream, options);
// Save output as PDF format
pdfDocument.Save(dataDir + "WebpageToPdf.pdf");
1 Like

@asad.ali

I have the same exception (System.ArgumentException: Incompatible unit types) in your example on the line

// Load HTML file
Document pdfDocument = new Document(stream, options);

Please, provide any details about the possible solution as as soon as you will have enough information about this issue. Thank you for your response!

@h0las

We have recorded your concerns under the ticket and will let you know as soon as make some progress towards ticket resolution.

1 Like