Loading the PDF feed from a URL

Hi, I'm currently working on a view to display a PDF from a URL using PDF Viewer but I can't get it to work properly.
Below is the call of the PDF Viewer in the view

     <div class="row">
         <div class="col-10 py-4">
             <h3 class="titre-section mb-4">SHow PDF</h3>
             <div class="modal-body saisie-body formContent">
                 <ejs-pdfviewer id="container" style="height:600px" serviceUrl="/api/PdfContract" DocumentPath="<My URL>"></ejs-pdfviewer>
             </div>
         </div>
     </div>

Below is my Controller

     public class PdfContractController : Controller
    {

        private readonly IHostingEnvironment _hostingEnvironment;
        public PdfContractController(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }

        public IActionResult Index()
        {
            return View();
        }

        [AcceptVerbs("Post")]
        [HttpPost]
        [Route("api/[controller]/Load")]
        public IActionResult LoadDataPdf([FromBody] Dictionary<string, string> jsonData)
        {
            var pdfViewer = new PdfRenderer();
            MemoryStream stream = new MemoryStream();
            if (jsonData != null && jsonData.ContainsKey("document"))
            {
               if (bool.Parse(jsonData["isFileName"]))
               {
                   var fileName = jsonData["document"].Split(new[] { "://" }, StringSplitOptions.None)[0];
                   if (fileName == "http" || fileName == "https")
                   {
                       var webClient = new WebClient();
                       byte[] pdfDoc = webClient.DownloadData(jsonData["document"]);
                       stream = new MemoryStream(pdfDoc);
                   }
                   else
                   {
                       return Content(jsonData["document"] + " is not found");
                   }                   
               }
               else
               {
                   byte[] bytes = Convert.FromBase64String(jsonData["document"]);
                   stream = new MemoryStream(bytes);
               }
            }
            var jsonResult = pdfViewer.Load(stream, jsonData);
            return Content(JsonConvert.SerializeObject(jsonResult));
        }

        [AcceptVerbs("Post")]
        [HttpPost]
        [Route("api/[controller]/RenderPdfPages")]
        public IActionResult RenderPdfPages([FromBody] Dictionary<string, string> jsonObject)
        {
            var pdfViewer = new PdfRenderer();
            var jsonResult = pdfViewer.GetPage(jsonObject);
            return Content(JsonConvert.SerializeObject(jsonResult));
        }
    }

I join the complete controller.

Once I pass in my controller I have no error but only the PDF module with the spinner that runs in a loop.
Below you will find information about my project:
  • .NET Core 2.1
  • Syncfusion.EJ2.AspNet.Core 18.4.0.46
  • Syncfusion.EJ2.PdfViewer.AspNet.Core.Windows 17.2.0.36 (last version that installs on Core 2.1)
I found on the forum an example of code that works but it is in .NET Core 2.2 and for support reasons I can't change the version of .NET.
Is there an error in my code or did I forget something in my code ?

Thank you in advance for your answers.
Quentin


Attachment: contract_e27ef6cb.rar

6 Replies

DM Dhivyabharathi Mohan Syncfusion Team March 5, 2021 11:28 AM UTC

Hi Chaffois, 
 
Thank you for contacting Syncfusion support. 
 
We will store the document in the cache based on the hashid during the initial loading of the pdf file. Then on scrolling or navigating to that page we will send the hashid and required page number from the client-side and then we will retrieve the page details with the hashid on the server-side. So the cache is required for rendering the PDF pages in our PDF Viewer control. We have checked the attached controller file, In that cache is missed in all the controller methods. So, that the issue occurs. We request you to use the _cache in all the controller method as like below code snippet. And we have shared by modifying the controller file which you have shared. 
 
 
 
  
  
  private IMemoryCache _cache;  
  
        public PdfViewerController(IHostingEnvironment hostingEnvironment, IMemoryCache cache)  
        {  
            _hostingEnvironment = hostingEnvironment;  
            _cache = cache;  
        }  
public IActionResult Load([FromBody] Dictionary<stringstring> jsonObject)  
        {  
            PdfRenderer pdfviewer = new PdfRenderer(_cache);  
            MemoryStream stream = new MemoryStream();  
            object jsonResult = new object();  
            if (jsonObject != null && jsonObject.ContainsKey("document"))  
            {  
  
                if (bool.Parse(jsonObject["isFileName"]))  
                {  
                    //Get the PDF document from Database  
                    byte[] docData = GetDocument(jsonObject["document"]);  
                    stream = new MemoryStream(docData);  
                }  
                else  
                {  
                    byte[] bytes = Convert.FromBase64String(jsonObject["document"]);  
                    stream = new MemoryStream(bytes);  
                }  
            }  
            jsonResult = pdfviewer.Load(stream, jsonObject);  
            return Content(JsonConvert.SerializeObject(jsonResult));  
        }  
  
  
  
 
 
Add the below line in startup.cs file  
  
  
public void ConfigureServices(IServiceCollection services)  
        {  
            services.AddMemoryCache();  
            services.AddMvc().  
           AddJsonOptions(options =>  
           {  
                // JSON serialization not defaulting to default?  
                options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();  
           });  
        }  
  
  
 
 
  
Kindly try it and let us know, if you still have any concerns. 
  
 
Regards,  
Dhivya. 


CQ Chaffois Quentin March 5, 2021 01:22 PM UTC

Hi Dhivya and thanks for the answer.

However I can't use the cache, because the version of Syncfusion.EJ2.PdfViewer.AspNet.Core.Windows that I use PdfRender doesn't accept any parameter, so if I add the cache as a parameter I get an error.
As mentioned in my question I'm using .NET Code 2.1 which doesn't support any version higher than 17.2.0.36 of Syncfusion.EJ2.PdfViewer.AspNet.Core.Windows.
Do I need an additional library for the generation to work ?

Thanks
Quentin


DM Dhivyabharathi Mohan Syncfusion Team March 8, 2021 12:04 PM UTC

Hi Chaffois, 
 
We were able to reproduce the reported issue with the provided details. We will provide the further details on March 10th, 2021. 
 
Regards, 
Dhivya. 



DM Dhivyabharathi Mohan Syncfusion Team March 10, 2021 02:10 PM UTC

Hi Chaffois, 
We are unable to reproduce the reported issue “load file from URL” in the 17.2.0.36 version. We suspect that the reported issue occurs due to the mismatch of the method name in the controller file, In the provided code-snippet the method name is LoadDataPdf but it has to be Load. We have also shared the sample for the same. Kindly download it from the below link. 
 
 
 
Can you please share the simple sample in which you have faced the issue or modify the provided sample along with the response retrieved in the network tab. So, that would be helpful for us to analyze more and assist you better 
 
 
Note: The error “PdfRender doesn't accept any parameter, so if I add the cache as a parameter” occurs on passing the cache as a parameter in the PdfRender object because we don’t have support for passing the cache in the parameter in the 17.2.0.36 version. 
 
Regards, 
Dhivya. 



CQ Chaffois Quentin March 10, 2021 02:55 PM UTC

HI

I just tested the example project you attached to your answer which works fine. I duplicated your code in my application. At runtime I don't get the error message about the cache, but the PDF doesn't display. I have the PDF loading spinner running without displaying anything.
After research the difference between the two applications is the version of Syncfusion.EJ2.PDFViewer.AspNet.Core.Windows installed.
On my application the version is 18.4.0.47
On the example application the version is 17.2.0.36
Can the problem come from an incompatibility at the version level?
Below is a reminder of the versions of the elements in my application:
Microsoft.AspNetCore.App => 2.1.1
Syncfusion.EJ2.AspNet.Core => 18.4.0.46
Syncfusion.EJ2.PDFViewer.AspNet.Core.Windows => 17.2.0.36

Thanks for your answers.

Quentin



DM Dhivyabharathi Mohan Syncfusion Team March 11, 2021 12:13 PM UTC

Hi Chaffois, 
 
We have created a new incident under your Direct Trac account. We suggest you follow up with the incident for further updates. Please log in using the below link.   
   
 
Regards, 
Dhivya. 


Loader.
Up arrow icon