Search code examples
asp.net-coreazure-functionsselectpdf

Conversion error: Could not open url. Select.HtmlToPdf.NetCore


I am evaluating Select.HtmlToPdf.NetCore to convert Html string to Pdf in my C# Azure function app.

I was trying SelectPdf-HtmlToPdf-Samples-v23.2.0 downloaded from Selectpdf website especially the Azure function.

I tried invoking the Azure function running from a local Visual Studio instance but getting an error upon executing;

doc = converter.ConvertHtmlString(html);

I am not using a base url here but I am getting the error

Conversion error: Could not open url.

Since I am not using a base url as a parameter, not sure what is causing this error.


Solution

  • Conversion error: Could not open url. Select.HtmlToPdf.NetCore

    Below code works fine for me:

    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Azure.Functions.Worker;
    using Microsoft.Extensions.Logging;
    using SelectPdf;
    
    namespace FunctionApp121
    {
        public class Function1
        {
            private readonly ILogger<Function1> _logger;
            public Function1(ILogger<Function1> logger)
            {
                _logger = logger;
            }
            [Function("Function1")]
            public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
            {
                _logger.LogInformation("Hello Rithwik, C# Code has started executing");
                PdfDocument testrith = new PdfDocument();
                HtmlToPdf converith = new HtmlToPdf();
                string testhtml = "<html><body>Hello Rithwik Bojja!</body></html>";
                testrith = converith.ConvertHtmlString(testhtml);
                MemoryStream rithpdf = new MemoryStream();
                testrith.Save(rithpdf);
                testrith.Close();
                rithpdf.Seek(0, SeekOrigin.Begin);
                _logger.LogInformation("Hello Rithwik,Execution Completed");
                return new FileStreamResult(rithpdf, "application/pdf")
                {
                    FileDownloadName = "rithwiktest.pdf"
                };
            }
        }
    }
    
    

    csproj:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <AzureFunctionsVersion>v4</AzureFunctionsVersion>
        <OutputType>Exe</OutputType>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
      <ItemGroup>
        <FrameworkReference Include="Microsoft.AspNetCore.App" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.1" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" />
        <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" />
        <PackageReference Include="Select.HtmlToPdf" Version="23.2.0" />
        <PackageReference Include="Select.HtmlToPdf.NetCore" Version="23.1.0" />
      </ItemGroup>
      <ItemGroup>
        <None Update="host.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>
      </ItemGroup>
      <ItemGroup>
        <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
      </ItemGroup>
    </Project>
    

    Output:

    enter image description here

    enter image description here

    Alternatively, html sent as body when calling function:

    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Azure.Functions.Worker;
    using Microsoft.Extensions.Logging;
    using SelectPdf;
    
    namespace FunctionApp121
    {
        public class Function1
        {
            private readonly ILogger<Function1> _logger;
            public Function1(ILogger<Function1> logger)
            {
                _logger = logger;
            }
            [Function("Function1")]
            public async Task<IActionResult> RunAsync([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
            {
                _logger.LogInformation("Hello Rithwik, C# Code has started executing");
                string testhtml = await new StreamReader(req.Body).ReadToEndAsync();
    
                if (string.IsNullOrEmpty(testhtml))
                {
                    return new BadRequestObjectResult("Please provide HTML content in the request body.");
                }
                PdfDocument testrith = new PdfDocument();
                HtmlToPdf converith = new HtmlToPdf();
                testrith = converith.ConvertHtmlString(testhtml);
                MemoryStream rithpdf = new MemoryStream();
                testrith.Save(rithpdf);
                testrith.Close();
                rithpdf.Seek(0, SeekOrigin.Begin);
                _logger.LogInformation("Hello Rithwik,Execution Completed");
                return new FileStreamResult(rithpdf, "application/pdf")
                {
                    FileDownloadName = "rithwiktest.pdf"
                };
            }
        }
    }
    

    Output:

    enter image description here