Reputation: 66
I am writing a Azure Function for PDF conversion with dependencies on DataLogics PDF conversion and a Nuget package (mlkpwgen) for password generation.
Functions are
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json;
using System;
using MlkPwgen;
using Datalogics.PDFL;
using System.Diagnostics;
namespace FunctionApp1
{
public static class Function1
{
[FunctionName("Function1")]
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
PDFConversion();
string requestBody = new StreamReader(req.Body).ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
public static string PDFConversion()
{
using (Library lib = new Library())
{
String sInput = @"C:\Users\Kunal\Downloads\Indian Management.pdf";
String sOutput = @"C:\Users\Kunal\Downloads\WatermarkedOutput.pdf";
Document doc = new Document(sInput);
string ownerPassword = PasswordGenerator.Generate(length: 32);
string userPassword = PasswordGenerator.Generate(length: 32);
doc.Secure(PermissionFlags.Print | PermissionFlags.HighPrint, ownerPassword, userPassword);
WatermarkParams watermarkParams = new WatermarkParams();
watermarkParams.Rotation = 45.3f;
watermarkParams.Opacity = 0.15f;
watermarkParams.TargetRange.PageSpec = PageSpec.AllPages;
WatermarkTextParams watermarkTextParams = new WatermarkTextParams();
Color color = new Color(0.0f / 255.0f, 0.0f / 255.0f, 0.0f / 255.0f);
watermarkTextParams.Color = color;
watermarkTextParams.Text = "Centre Code - Unit - 0101";
Font f = new Font("Arial", FontCreateFlags.Embedded | FontCreateFlags.Subset);
watermarkTextParams.Font = f;
watermarkTextParams.FontSize = 80f;
watermarkTextParams.TextAlign = HorizontalAlignment.Center;
doc.Watermark(watermarkTextParams, watermarkParams);
doc.EmbedFonts();
doc.Save(SaveFlags.Full | SaveFlags.Linearized, sOutput);
Process.Start(@"C:\Users\Kunal\Downloads\WatermarkedOutput.pdf");
return sInput;
}
}
}
}
I am getting the following Exception
"System.Private.CoreLib: Exception while executing function: Function1. Datalogics.PDFL: The type initializer for 'Datalogics.PDFL.PDFLPINVOKE' threw an exception. Datalogics.PDFL: The type initializer for 'SWIGExceptionHelper' threw an exception. Datalogics.PDFL: Unable to load DLL 'DL150PDFLPINVOKE': The specified module could not be found. (Exception from HRESULT: 0x8007007E)."
The same code works fine as a Console application. What am I missing here?
Upvotes: 5
Views: 18706
Reputation: 1215
Per this MS Forums post:
Azure Functions does not provide support for loading native binaries in its current release. Even if we were able to install this package, you may still encounter errors when those native dlls are loaded during runtime.
So this is expected behavior when trying to call native binaries. Please contact our Support department if you have any more questions about getting started using the PDF Library.
Upvotes: 0
Reputation: 66
Thanks for reading through the question and trying to answer.
I found that even after adding reference to the Datalogics.PDFL.dll, the code was failing.
So i copied all the other dll's into the bin\debug folder and now the code works fine
DL150ACE.dll
DL150AdobeXMP.dll
DL150AGM.dll
DL150ARE.dll
DL150AXE8SharedExpat.dll
DL150BIB.dll
DL150BIBUtils.dll
DL150CoolType.dll
DL150JP2KLib.dll
DL150PDFL.dll
DL150PDFLPINVOKE.dll
DL150pdfport.dll
DL150pdfsettings.dll
DotNETViewerComponent.dll
Upvotes: 0
Reputation: 35134
If fixing the hard-coded file names still doesn't help, the error sounds like a permission exception.
Azure Functions run on App Service, which has a sandbox for all the code, where some calls are not allowed. E.g. GDI32 which is used extensively by PDF generation libraries.
Read more in Azure Web App sandbox.
Upvotes: 1