pdfviewer doesn't communicate with pdfviewer-web-service

Hi, i have an application with SfPDFviewer, that listen to a web service ( https://www.syncfusion.com/kb/11063/how-to-create-pdf-viewer-web-service-in-net-core-3-0-and-above )

and i'm having the following problem


any idea to solve this?

my code: 

@inject IPierTarefaWebService _pdsTarefa

@inject IConfiguration Configuration

<SfPdfViewer Width="100%" Height="100%" @ref="@PDF" DocumentPath=@DocumentPath

             ServiceUrl=@Configuration["PdfUrl"]></SfPdfViewer>

@code{

    [Parameter]

    public string ReportPath { get; set; }


    public byte[] byteArray { get; set; }

    public string base64String { get; set; } = "";

    public string DocumentPath { get; set; }

    private SfPdfViewer PDF = new SfPdfViewer();

    protected override async Task OnInitializedAsync()

    {

        byteArray = System.IO.File.ReadAllBytes(ReportPath);

        base64String = Convert.ToBase64String(byteArray);

        DocumentPath = "data:application/pdf;base64," + base64String;

    }

}

the service url =

"PdfUrl": "http://localhost:5000/pdfviewer"

the webservice running: 




7 Replies

DM Dhivyabharathi Mohan Syncfusion Team February 9, 2022 09:58 AM UTC

Hi Mateus, 
 
We have checked with the Blazor Web Assembly and Asp.Net Core web service. The document is loaded properly in the PDF Viewer and the reported issue is not replicated on our end. We have shared the Blazor sample and Asp.Net Core web service in which we have tried to replicate the reported issue. And we suspect that the reported issue occurs due to the CORS is not mapped properly in the web service. So, the reported issue occurs. We suggest you add the below code snippets in the startup.cs file to get the issue resolved. 
 
 
 
 
 
Code snippets to add in Startup.cs file: 
readonly string MyAllowSpecificOrigins = "MyPolicy"; 
services.AddCors(options => 
            { 
                options.AddPolicy(MyAllowSpecificOrigins, 
                builder => 
                { 
                    builder.AllowAnyOrigin() 
                      .AllowAnyMethod() 
                      .AllowAnyHeader(); 
                }); 
            }); 
 
 
 
 
Kindly try this and let us know whether the reported issue is resolved or not. If the issue still not resolved, then kindly share the modified web service in which you are facing the reported issue. It will be helpful for us to investigate further and provide the solution at the earliest. 
 
Regards, 
Dhivya. 



MV Mateus Viana Alves February 9, 2022 02:52 PM UTC

i tested this and also changed the service url to https on port 5001, and that solved the reported problem, it shows the pdf correctly, but it generated probems in the unload function, when i close the pdfviewer ( SyncfusionBlazor version: 19.4.0.48)




in some cases it dosen't show the pdf(bigger than the others) and generates the error below:



i'm attaching the web service code.


Attachment: pdfviewerservice_2541979e.zip



DM Dhivyabharathi Mohan Syncfusion Team February 10, 2022 12:08 PM UTC

Hi Mateus, 
  
 
You have used the HTTP post [HttpPost("Unload")] over the importFormFields and ExportFormFields web actions in the PdfViewerController.cs file. So, the request for Unload from the Client side is not accepted properly and the issue occurs. We suggest you use the HTTP Post over the web action methods based on the method name to get the issue resolved. We have shared the modified web service, code snippet and KB link for your reference.  
  
  
Code snippet:  
  
[AcceptVerbs("Post")]  
        [HttpPost("ExportFormFields")]  
        [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")]  
        [Route("[controller]/ExportFormFields")]  
        public IActionResult ExportFormFields([FromBody] Dictionary<stringstring> jsonObject)  
  
        {  
            PdfRenderer pdfviewer = new PdfRenderer(_cache);  
            PdfRenderer.ReferencePath = _hostingEnvironment.WebRootPath + "\\pdfium.dll";  
            string jsonResult = pdfviewer.ExportFormFields(jsonObject);  
            return Content(jsonResult);  
        }  
  
        [AcceptVerbs("Post")]  
        [HttpPost("ImportFormFields")]  
        [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")]  
        [Route("[controller]/ImportFormFields")]  
        public IActionResult ImportFormFields([FromBody] Dictionary<stringstring> jsonObject)  
        {  
            PdfRenderer pdfviewer = new PdfRenderer(_cache);  
            PdfRenderer.ReferencePath = _hostingEnvironment.WebRootPath + "\\pdfium.dll";  
            jsonObject["data"] = GetDocumentPath(jsonObject["data"]);  
            object jsonResult = pdfviewer.ImportFormFields(jsonObject);  
            return Content(JsonConvert.SerializeObject(jsonResult));  
        }  
  
        [AcceptVerbs("Post")]  
        [HttpPost("Unload")]  
        [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")]  
        [Route("[controller]/Unload")]  
        //Post action for unloading and disposing the PDF document resources    
        public IActionResult Unload([FromBody] Dictionary<stringstring> jsonObject)  
        {  
            //Initialize the PDF Viewer object with memory cache object  
            PdfRenderer pdfviewer = new PdfRenderer(_cache);  
            PdfRenderer.ReferencePath = _hostingEnvironment.WebRootPath + "\\pdfium.dll";  
            pdfviewer.ClearCache(jsonObject);  
            return this.Content("Document cache is cleared");  
        }  
  
  
  
  
  
  
  
Kindly try this and revert us, if you have any concerns.  
 
 
Regards, 
Dhivya. 



MV Mateus Viana Alves February 10, 2022 06:54 PM UTC

Hi,

This solved one of the problems that was generated in the console, but some errors remain. After further analysis, i found that they were generated after i updated Syncfusion Blazor from version 19.2.0.44 to 19.4.0.48 and Microsoft.AspNetCore.Components.Web from version 5.0.6 to 6.0.2.


UPDATE 1:

i tested version 19.3.0.43 and this version doesn't have error, i also tested version 19.3.0.49 and it gives me the error "Connect disconnected" in some cases, i believe the problem is between these versions.






DM Dhivyabharathi Mohan Syncfusion Team February 11, 2022 11:10 AM UTC

Hi Mateus, 
 
The reported issue is not replicated on our end in the v19.3.0.49 and with the latest version v19.4.0.48. We suspect that you have tried to access the method or property of the PDF Viewer using the PDF Viewer instance before the PDF Viewer component is created. So, the reported issue occurs. Could you please share the modified Blazor application in which you are facing this issue? It will be helpful for us to investigate further and provide the solution at the earliest. 
 
Regards, 
Dhivya. 



MV Mateus Viana Alves February 11, 2022 02:30 PM UTC

my application is server side, and i couldn't use SfPdfViewerServer component because it generates errors in others projects in the same solution, i chose SfPdfViewer, could this be the problem?

i try to reply te error in this application test


Attachment: BlazorServer_with_PdfViewer_1a8ff8bc.zip


DM Dhivyabharathi Mohan Syncfusion Team February 14, 2022 06:42 AM UTC

Hi Mateus, 
 
We have checked the sample you shared. In this you have used the Syncfusion.Blazor NuGet package instead of Syncfusion.Blazor.PdfViewerServer.Windows. And you have missed the below code snippets in the Statup.cs file. So, the reported issue occurs. We have shared the code snippets and modified sample for your reference. 
 
 
Code snippets: 
 
services.AddServerSideBlazor().AddHubOptions(o => { o.MaximumReceiveMessageSize = 102400000; }); 
                         
 
 
 
 
Please try this and revert to us, if you need further assistance. 
Regards, 
Dhivya. 


Loader.
Up arrow icon