How to add an authentication header to the image requests that are in the html using Aspose.PDF for .NET

Is there a way to add authentication header to the image requests that are in the html.??? Currently the document includes the images from the image src defined in Html as long as authentication header is not needed. Could you help me with the code.??

//this is my converter method

public List<byte[]> Convert(ContentsDto content, IDictionary<string, string> options, System.Func<DependentContent, Task<ContentsDto>> GetDependency = null)
    {

        var pdfBytearray = new List<byte[]>();
        License htmlLicense = new License();
       
        //authentication
        WebClient webClient = new WebClient();
        AssetteAuthenticationHelper assetteAuthenticationHelper = new AssetteAuthenticationHelper(_authSettings, _settingsProvider.User.UserId, _settingsProvider.User.ClientId);
        var token = assetteAuthenticationHelper.GetAuthToken();
        webClient.Headers.Add("Authorization", $"Bearer {token}");
      
        using (var streamProvider = new MemoryStream(content.Data))
        {
            //convert html to pdf
            HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions() { HtmlMediaType = HtmlMediaType.Print };
            Document pdfDocument = new Document(streamProvider, htmlLoadOptions );

            //convert pdf to pptx 
            PptxSaveOptions pptx_save = new PptxSaveOptions();
            using (var pptxStream = new MemoryStream())
            {
                pdfDocument.Save(pptxStream, pptx_save);
                pptxStream.Seek(0, System.IO.SeekOrigin.Begin);
                pdfBytearray.Add(pptxStream.ToArray());
            }
        }
        return pdfBytearray;
    }

@nethmi

Sadly, the authentication process is not done by the Aspose.PDF API during HTML to PDF Conversion. In case images are not being downloaded by WebClient in the first place, we are afraid that we cannot help in this case. However, if images are coming in the HTML String downloaded by WebClient and API is unable to include it in the PDF during conversion, we would need to investigate the issue. Please share the sample HTML string or file with us in .zip format so that we can perform testing in our environment and address it accordingly.

1 Like