I am getting the following error when running the published version of my function, it should be noted that when I run this locally it works correctly.
Access to the path 'D:\\Program Files (x86)\\SiteExtensions\\Functions\\1.0.12205\\wkhtmltopdf' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj, Boolean checkHost) at System.IO.Directory.InternalCreateDirectoryHelper(String path, Boolean checkHost) at System.IO.Directory.CreateDirectory(String path) at NReco.PdfGenerator.HtmlToPdfConverter.EnsureWkHtmlLibs() at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdfInternal(WkHtmlInput[] htmlFiles, String inputContent, String coverHtml, String outputPdfFilePath, Stream outputStream) at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdf(String htmlContent, String coverHtml, Stream output) at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdf(String htmlContent, String coverHtml) at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdf(String htmlContent) at HTMLtoPDF.Function1.d__0.MoveNext()
My application basically transforms HTML into a PDF file, uploads to azure and returns the result uri.
As you can see in my code below I don't make any references to this path.
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using NReco.PdfGenerator;
using System;
namespace HTMLtoPDF
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
string pdfurl = "";
// parse query parameter
string html = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "html", true) == 0)
.Value;
if (html == null)
{
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
html = data?.html;
}
try
{
HtmlToPdfConverter converter = new HtmlToPdfConverter();
var genpdf = converter.GeneratePdf(html);
var stream = new MemoryStream(genpdf, writable: false);
pdfurl = await UploadFileAsBlobAsync(stream, Guid.NewGuid()+".pdf");
}
catch (Exception ex)
{
pdfurl = ex.Message+Environment.NewLine+ex.InnerException;
}
return html == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass html on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, pdfurl);
}
public static async Task<string> UploadFileAsBlobAsync(Stream stream, string filehtml)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(HIDDENFORSECURITY);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference("applicationimages");
CloudBlockBlob blockBlob = container.GetBlockBlobReference(filehtml);
await blockBlob.UploadFromStreamAsync(stream);
stream.Dispose();
return blockBlob?.Uri.ToString();
}
}
}
Looks like NReco PdfConverter uses wkhtmltopdf.exe under the hood to generate the PDF and when calling NReco.PdfGenerator.HtmlToPdfConverter.EnsureWkHtmlLibs()
it is getting the access denied exception.
See here for possible resolution: https://github.com/nreco/nreco/issues/4
The problem is in below two lines. It is trying to create folders on file system. When you host your program in Azure, folder permitted
HtmlToPdfConverter converter = new HtmlToPdfConverter();
var genpdf = converter.GeneratePdf(html);
Can you use the stream variant of GeneratePdf which dos not generate any intermittent folder.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.