PdfRenderer.ReferencePath = _hostingEnvironment.WebRootPath + "\\"; |
public IActionResult Load([FromBody] Dictionary<string, string> jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
PdfRenderer.ReferencePath = _hostingEnvironment.WebRootPath + "\\";
MemoryStream stream = new MemoryStream();
object jsonResult = new object();
if (jsonObject != null && jsonObject.ContainsKey("document"))
{
if (bool.Parse(jsonObject["isFileName"]))
{
string documentPath = GetDocumentPath(jsonObject["document"]);
if (!string.IsNullOrEmpty(documentPath))
{
byte[] bytes = System.IO.File.ReadAllBytes(documentPath);
stream = new MemoryStream(bytes);
}
else
{
return this.Content(jsonObject["document"] + " is not found");
}
}
else
{
byte[] bytes = Convert.FromBase64String(jsonObject["document"]);
stream = new MemoryStream(bytes);
}
}
jsonResult = pdfviewer.Load(stream, jsonObject);
return Content(JsonConvert.SerializeObject(jsonResult));
} |
RUN apt-get update \
&& apt-get install -y --allow-unauthenticated \
libc6-dev \
libgdiplus \
libx11-dev \
curl \
vim \
supervisor \
procps
|
Hi Christof,
Thank you for contacting Syncfusion support.
Based on the provided details we suspect that the write access permission is denied so the exception is thrown. We have embedded the Pdfium rendering engine in our PDF Viewer for robust rendering, so Pdfium dll will be generated on runtime within your project location.However, we have exposed the ReferencePath API to set the pdfium library location path. We can place the pdfium library in project location and refer the project location to the ReferencePath. Please find the below code to set the pdfium location inside the wwwroot folder.Code snippet:
PdfRenderer.ReferencePath = _hostingEnvironment.WebRootPath + "\\";Example:
public IActionResult Load([FromBody] Dictionary<string, string> jsonObject){PdfRenderer pdfviewer = new PdfRenderer(_cache);PdfRenderer.ReferencePath = _hostingEnvironment.WebRootPath + "\\";MemoryStream stream = new MemoryStream();object jsonResult = new object();if (jsonObject != null && jsonObject.ContainsKey("document")){if (bool.Parse(jsonObject["isFileName"])){string documentPath = GetDocumentPath(jsonObject["document"]);if (!string.IsNullOrEmpty(documentPath)){byte[] bytes = System.IO.File.ReadAllBytes(documentPath);stream = new MemoryStream(bytes);}else{return this.Content(jsonObject["document"] + " is not found");}}else{byte[] bytes = Convert.FromBase64String(jsonObject["document"]);stream = new MemoryStream(bytes);}}jsonResult = pdfviewer.Load(stream, jsonObject);return Content(JsonConvert.SerializeObject(jsonResult));}We have also shared the Pdfium dll in the below link,Note:Kindly use the Syncfusion.EJ2.PdfViewer.AspNet.Core.Linux package in your application for linux environment . Also, ensure whether the library dependencies of libpdfium.so are installed properly. If not, please execute the following command to install the dependencies
RUN apt-get update \&& apt-get install -y --allow-unauthenticated \libc6-dev \libgdiplus \libx11-dev \curl \vim \supervisor \procps
Please try this and revert us with more details if you are still facing the issue and confirm us whether you are hosting the application in Windows or Linux environment. These details will be helpful for us to investigate further and assist you better.
Regards,Akshaya
PdfRenderer pdfviewer = new PdfRenderer(_cache); |
Hello, I am facing similar issues getting the PdfViewer to work in AWS Lambda. Do you have any updates on this? Thanks
I have tried these steps in deploying the server to Google App Engine with no luck.
I am also receiving this issue. I have a blazor wasm client and webapi.
jsonResult = pdfviewer.Load(stream, jsonObject);
var content = JsonConvert.SerializeObject(jsonResult);
b.AppendLine($"jsonResult: {content}");
return Content(content);
My files are located on a network path. They cannot be mounted to the IIS application. I am looking to simply load the bytes and send them back to the client, a stream or other chunking solution would be preferred.
Logging this out to my event log clearly shows that I load the bytes. However; the serialization operation fails with the error: "The type initializer for 'Syncfusion.EJ2.PdfViewer.PdfiumNative' threw an exception."
public IActionResult Load([FromBody] Dictionary<string, string> jsonObject)
{
StringBuilder b = new StringBuilder();
try
{
Console.WriteLine("Load called");
//Initialize the PDF viewer object with memory cache object
PdfRenderer pdfviewer = new PdfRenderer(_cache);
if (!Debugger.IsAttached)
PdfRenderer.ReferencePath = _hostingEnvironment.WebRootPath + "\\";
else
PdfRenderer.ReferencePath = AppDomain.CurrentDomain.BaseDirectory;
MemoryStream stream = new MemoryStream();
object jsonResult = new object();
if (jsonObject != null && jsonObject.ContainsKey("document"))
{
b.AppendLine("Contains key 'document'.");
if (bool.Parse(jsonObject["isFileName"]))
{
b.AppendLine("is a file name");
string documentPath = GetDocumentPath(jsonObject["document"]);
b.AppendLine($"working with file: {documentPath}");
b.AppendLine($"documentPath isnull = {string.IsNullOrEmpty(documentPath)}, exists = {System.IO.File.Exists(documentPath)}");
if (!string.IsNullOrEmpty(documentPath) && System.IO.File.Exists(documentPath))
{
byte[] bytes = System.IO.File.ReadAllBytes(documentPath);
b.AppendLine($"byteCount: {bytes.Length}");
stream = new MemoryStream(bytes);
}
else
{
b.AppendLine($"unable to find: {documentPath}");
throw new FileNotFoundException("document no found.");
//return this.Content(jsonObject["document"] + " is not found");
}
}
else
{
b.AppendLine($"is not a file name");
byte[] bytes = Convert.FromBase64String(jsonObject["document"]);
stream = new MemoryStream(bytes);
}
}
jsonResult = pdfviewer.Load(stream, jsonObject);
var content = JsonConvert.SerializeObject(jsonResult);
b.AppendLine($"jsonResult: {content}");
return Content(content);
}
catch (FileNotFoundException)
{
b.AppendLine($"error file not found.");
return this.Content($"The documentId: {jsonObject["document"]} was not found.");
}
catch (ArgumentOutOfRangeException)
{
b.AppendLine($"error argument out of range.");
return this.Content($"The documentId: {jsonObject["document"]} is an invalid document identifier.");
}
catch (Exception ex)
{
b.AppendLine($"error unexpected.\r\n{ex.Message}");
return this.Content($"An unexpted error occurred loading {jsonObject["document"]}.");
}
finally {
_logger.LogWarning($"Load Document Actions:\r\n{b}");
}
}
The event log out is:
Load Document Actions:
Contains key 'document'.
is a file name
working with file: \\NCC-1701\File Services\Warehouses\TSCC\00000000\5abb3271afb94061bf55db1334816282.pdf
documentPath isnull = False, exists = True
byteCount: 81885
jsonResult: "The type initializer for 'Syncfusion.EJ2.PdfViewer.PdfiumNative' threw an exception."
Hi Matthew,
We suspect that issue is due to pdfium.dll is not referred properly. We will create the pdfium.dll based on
the operating system at the runtime. So, could you please try
copying the below provided x64 and x86 folder and paste inside the folder into
your project to resolve the issue ?
https://www.syncfusion.com/downloads/support/directtrac/general/ze/pdfium956869027.zip
Note:
· Kindly use both the client and server-side of the same version in your project
· Ensure whether the Pdfium dll will be created in your project during runtime. Else we can also place the pdfium assemblies in any of the production environment locations and refer to the path by using the ReferencePath API.
For example:
If the Pdfium assembly is available in this path C:\Pdfium\x64 or D:\Pdfium\x86, the reference path should be PdfRenderer.ReferencePath = "C:/";
The parent folder has to be provided as the path in the ReferencePath API. Note: Provide this path in the Load method of the PDFViewerController.cs PdfRenderer PdfRenderer = new PdfRenderer(); PdfRenderer.ReferencePath = @"C:/"; |
Steps to refer the Pdfium.dll:
1.Extract the given file (Pdfium folder) and copy it in the sample’s root directory (parallel to Controllers folder)
2. Right-click on the pdfium.dll (both the X64 andX86 folder) and then choose the “Copy to Output Directory” property from the property window and set its value to “copy always”. This setting will ship the pdfium.dll assembly to the published location.
Screenshot:
3. Provide this path in the Load method of the PDFViewerController.cs
Note: You need to refer the ParentFolder up to x64/x86 folder.
PdfRenderer.ReferencePath = _hostingEnvironment.ContentRootPath + \\Pdfium\\; |
4. Build and publish the application.
Also please install
only the package related to that OS, then build and run the project in that
platform. For Windows, Linux and OSX operating systems, use the following
corresponding libraries:
Kindly try this and let us know if the issue resolves or not.
Regards,
Vasugi
Hello,
I am an intern developer who is very new to .NET but am having a very similar issue to this one and have tried everything mentioned in the thread and it has worked great for me on Windows but not my Mac. I have set the ReferencePath to /Pdfium/ relative to content root, and have folders x64/ and x86/, each with the correct .dll files. As well I have installed Syncfusion.EJ2.PdfViewer.AspNet.Core.Windows (all of this on Windows) and it works. On Mac, I have an ARM processor which is likely the issue but I am unsure of how to proceed. I have similar x64/ and x86/ folders with the provided libpdfium.dylib in both folders, as well as Syncfusion.EJ2.PdfViewer.AspNet.Core.OSX installed, and the Windows package removed. When I start the local server and PdfRenderer.ReferencePath gets set, it creates a NEW folder 'x64\' inside of /Pdfium/ that generates pdfium.dll inside of it, and I assume that my Mac cannot read this file, and possibly can't even access the folder. I am wondering either if there is a way for me to force PdfRenderer to recognize the x64 folder that already exists or if I can change something such that the generated file is correct for my platform.
Apologies if anything I've mentioned is obviously solved or if I'm asking the wrong questions. I'm very new to this and would greatly appreciate any help, as it is difficult for me to consistently work on Windows and I'd much prefer to develop on my Mac.
Thank you,
Andrew
Currently, there is no support for ARM64 in the PDF Viewer component. We have already logged a feature request as “PDF Viewer should work in .NET 6.0 ARM64 on the Linux Platform”. But we don’t have immediate plans to implement this feature and it will be available in any of our future releases in 2023. You can track the status of the feature through the following link.
Feedback link: PDF Viewer should work in .NET 6.0 ARM64 on the Linux platform | Feature Feedback
I'm encountering an error while working with the ASP.NET Core PDF Viewer Web Service example provided by Syncfusion. The error message I'm seeing is:
"The type initializer for 'Syncfusion.PdfToImageConverter.PdfiumNative' threw an exception."
I've followed the instructions provided in the GitHub repository (https://github.com/SyncfusionExamples/EJ2-PDFViewer-WebServices/tree/main/ASP.NET%20Core/PdfViewerWebService_8.0) to set up the project and also followed the instruction given in this thread and Pdfium dll is generating on runtime within my project location (/EJ2-PDFViewer-WebServices/ASP.NET Core/PdfViewerWebService_8.0/PdfViewerWebService_8.0/wwwroot\/x64\/libpdfium.so)
Hi Ifra,
We recommend using
the "Syncfusion.EJ2.PdfViewer.AspNet.Core"NuGet
package, which is compatible with Windows, Linux, and OSX platforms and also,
please remove the Reference path and try once.
If you are using the PDF Viewer with Docker in .NET 6.0 framework, please refer to the following Dockerfile and modify your Dockerfile accordingly:
If you are using the PDF Viewer with Docker in .NET 8.0 framework, please refer to the following Dockerfile and modify your Dockerfile accordingly:
If you are still facing any issues, please provide the following details, which will help us analyze further and provide assistance:
Hello! I am facing similar issues getting the PdfViewer to work in AWS Lambda. Do you have any updates on this?
Thanks
Hi Saurabh,
Based on the given details, we suspect that the Pdfium
dependency is not installed in your environment. Please follow below steps to
install the Pdfium dependency:
sudo cp -u /lib/x86_64-linux-gnu/libdl.so.2 /lib/x86_64-linux-gnu/libdl.so sudo apt-get update sudo apt install libgdiplus |