I follow this sample: https://odetocode.com/blogs/scott/archive/2018/02/14/pdf-generation-in-azure-functions-v2.aspx
because I need to generate an image from HTML. I followed the instructions, downloaded the 32-bit binaries from DinkToPdf repository and added them to the project as seen below. Problem is I don't know how to do this part in particular:
I added the binary to my function app project and set the build action “Copy to Output Directory”. As we are about to see, the 32 bit address space is not a problem.
Why I don't have an option for "Copy to Output Directory" in my IDE?
And probably that's the reason I get the following exception:
Unable to load DLL 'libwkhtmltox' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)
on this fragment:
arr = pdfConverter.Convert(new HtmlToPdfDocument() { Objects = { new ObjectSettings { HtmlContent = html } } });
Here is my full code:
using DinkToPdf;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using IPdfConverter = DinkToPdf.Contracts.IConverter;
namespace Html2Img
{
public static class HtmlToImage
{
[FunctionName("HtmlToImage")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")]HttpRequest request, ILogger log)
{
log.LogInformation($"Converting {request.Query["url"]} to Image");
var html = await FetchHtml(request.Query["url"]);
var pdfBytes = BuildPdf(html);
var response = BuildResponse(pdfBytes);
return response;
}
private static FileContentResult BuildResponse(byte[] pdfBytes)
{
return new FileContentResult(pdfBytes, "application/jpg");
}
private static byte[] BuildPdf(string html)
{
byte[] arr = null;
try
{
arr = pdfConverter.Convert(new HtmlToPdfDocument() { Objects = { new ObjectSettings { HtmlContent = html } } });
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
return arr;
}
private static async Task<string> FetchHtml(string url)
{
var response = await httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
throw new InvalidOperationException($"FetchHtml failed {response.StatusCode} : {response.ReasonPhrase}");
}
string str = string.Empty;
try
{
string s = response.Headers.TransferEncoding.ToString();
str = await response.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
return str;
}
static HttpClient httpClient = new HttpClient();
static IPdfConverter pdfConverter = new SynchronizedConverter(new PdfTools());
}
User contributions licensed under CC BY-SA 3.0