We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Provide OAUTH2 Authorization Code Example C# to Convert HTML to PDF

Hi

I have a webpage protected by Microsoft Identity that I want to convert to PDF using C# and OAUTH for Authorization. Is this supported? Please provide OAUTH2 Authorization Code Example in C#.

I have tried to set up all the app registrations and scopes and everything.
I have modified the sample code example to this:

// C# Code
    if (!string.IsNullOrEmpty(accessToken))
    {
        HtmlLoadOptions options = new HtmlLoadOptions(webApiUrl)
        {
            PageInfo = { Width = 842, Height = 1191, IsLandscape = true }
        };

        var defaultRequestHeaders = HttpClient.DefaultRequestHeaders;
        defaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

        HttpResponseMessage response = await HttpClient.GetAsync(webApiUrl);
        if (response.IsSuccessStatusCode)
        {
            var content = await response.Content.ReadAsStreamAsync();

            Document pdfDocument = new Document(content, options);
            pdfDocument.Save($"test.PDF");

            StreamReader reader = new StreamReader(content);
            string text = reader.ReadToEnd();
            Console.WriteLine(text);
        }
        else
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"Failed to call the web API: {response.StatusCode}");
            string content = await response.Content.ReadAsStringAsync();

            // Note that if you got reponse.Code == 403 and reponse.content.code == "Authorization_RequestDenied"
            // this is because the tenant admin as not granted consent for the application to call the Web API
            Console.WriteLine($"Content: {content}");
        }
    }

But the PDF document remains empty. Tried to print out the HTML String returned by the httpClient and it is the right content. If OAuth is supported - could you please show an example of how to do it?

@ClausK2

The Aspose.PDF does not provide any method to download HTML using OAUTH. It offers the functionality to Convert HTML to PDF using C# only. You can either use HTML String or .html file to convert it into PDF.

Could you please share the complete HTML string that you are able to print out correctly? You can share it by creating a .html file as well and adding it to .zip format. We will try to convert it into PDF using Aspose.PDF and share our feedback with you.

Hi

Thanks for quick reply and for pointing me in the right direction.

It turns out the html had some errors that I thing i am able to fix

I may find a workaround but it would be nice to be able to send an OAUTH accesstoken for external resources instead of credentials in this example:

/Claus

@ClausK2

A feature request as PDFNET-50325 has been logged in our issue tracking system to provide the functionality to convert HTML to PDF using OAUTH2 Authorization Code in C#. We will further investigate the feasibility of this feature and let you know as soon as the ticket is resolved. Please be patient and spare us some time.

1 Like