BoldDeskWe are launching BoldDesk on Product Hunt soon. Learn more & follow us.
I used the code from the example
Client-side error is found. Please check the custom headers provided in the AjaxRequestSettings property and web action methods in the ServerActionSettings property.
Browser console
We
were unable to reproduce the reported issue with the provided details. We have
shared the sample in which we have tried to reproduce the issue in the below
link,
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/ASPNET_7.01853848264.zip
We were able to load in .Net 7.0 framework without any issues.
Try this sample and revert to us with the following details, if you have any concerns on this.
This
will be helpful for us to analyze further and provide the details.
Can I get the updated version of this exact same sample I am using 23.1.39 .net core 7 ejs pdf viewer and the Load method keeps getting called with null
Load called with: null
The code base I have is pretty much the same but a newer version of the package.
Any updates here would be much appreciated, thanks!
The status code 400 indicates that the PDF Viewer controller may not be configured correctly. However, we were unable to replicate the reported issue. We've provided a sample in version 23.1.39 at the following link, where we attempted to reproduce the issue:
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/CoreSample(23.1.39)209614464.zip
Kindly compare our sample and modify your sample accordingly. if you have any concerns on this, revert to us with the following details. This information will assist us in further analyzing the problem and providing you with the necessary details.
Hi the codebase I need is for a .net core MVC project not razor page based, could you please provide a sample for that.
So I have the ejs-pdfviewer as specified here using .net core package https://ej2.syncfusion.com/aspnetcore/documentation/pdfviewer/getting-started-with-server-backed
but because I have an mvc pattern and not razor page I tried using the controller specified here https://ej2.syncfusion.com/aspnetmvc/documentation/pdfviewer/getting-started-with-server-backed but the body isn't being read properly and there's a bunch of errors the most common being the server is not responding from the viewer and there is clear mismatch in request body between client -- server.
using Azure.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Syncfusion.EJ2.PdfViewer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web;
namespace Brevity.Controllers
{
public class PDFViewerController: Controller
{
private readonly IWebHostEnvironment _hostingEnvironment;
public PDFViewerController(IWebHostEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
[HttpPost]
public async Task<ActionResult> Load()
{
jsonObjects jsonObject = new jsonObjects();
Request.EnableBuffering();
Request.Body.Position = 0;
var rawRequestBody = await new StreamReader(Request.Body).ReadToEndAsync();
jsonObject = JsonConvert.DeserializeObject<jsonObjects>(rawRequestBody);
Console.WriteLine("loading " + rawRequestBody);
Console.WriteLine("loading " + jsonObject.document);
PdfRenderer pdfviewer = new PdfRenderer();
MemoryStream stream = new MemoryStream();
var jsonData = JsonConverter(jsonObject);
object jsonResult = new object();
if (jsonObject != null && jsonData.ContainsKey("document"))
{
if (bool.Parse(jsonData["isFileName"]))
{
string documentPath = GetDocumentPath(jsonData["document"]);
if (!string.IsNullOrEmpty(documentPath))
{
byte[] bytes = System.IO.File.ReadAllBytes(documentPath);
stream = new MemoryStream(bytes);
}
else
{
string fileName = jsonData["document"].Split(new string[] { "://" }, 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);
}
}
jsonResult = pdfviewer.Load(stream, jsonData);
Console.WriteLine(JsonConvert.SerializeObject(jsonResult));
return Content(JsonConvert.SerializeObject(jsonResult));
}
public Dictionary<string, string> JsonConverter(jsonObjects results)
{
Dictionary<string, object> resultObjects = new Dictionary<string, object>();
resultObjects = results.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(prop => prop.Name, prop => prop.GetValue(results, null));
var emptyObjects = (from kv in resultObjects
where kv.Value != null
select kv).ToDictionary(kv => kv.Key, kv => kv.Value);
Dictionary<string, string> jsonResult = emptyObjects.ToDictionary(k => k.Key, k => k.Value.ToString());
return jsonResult;
}
[HttpPost]
public ActionResult ExportAnnotations(jsonObjects jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
var jsonData = JsonConverter(jsonObject);
string jsonResult = pdfviewer.ExportAnnotation(jsonData);
return Content((jsonResult));
}
[HttpPost]
public ActionResult ImportAnnotations(jsonObjects jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
string jsonResult = string.Empty;
var jsonData = JsonConverter(jsonObject);
if (jsonObject != null && jsonData.ContainsKey("fileName"))
{
string documentPath = GetDocumentPath(jsonData["fileName"]);
if (!string.IsNullOrEmpty(documentPath))
{
jsonResult = System.IO.File.ReadAllText(documentPath);
}
else
{
return this.Content(jsonData["document"] + " is not found");
}
}
return Content(JsonConvert.SerializeObject(jsonResult));
}
[HttpPost]
public ActionResult<object> ImportFormFields(jsonObjects jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
var jsonData = JsonConverter(jsonObject);
object jsonResult = pdfviewer.ImportFormFields(jsonData);
return Ok(JsonConvert.SerializeObject(jsonResult));
}
[HttpPost]
public ActionResult ExportFormFields(jsonObjects jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
var jsonData = JsonConverter(jsonObject);
string jsonResult = pdfviewer.ExportFormFields(jsonData);
return Content(jsonResult);
}
[HttpPost]
public ActionResult RenderPdfPages(jsonObjects jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
var jsonData = JsonConverter(jsonObject);
object jsonResult = pdfviewer.GetPage(jsonData);
return Content(JsonConvert.SerializeObject(jsonResult));
}
[HttpPost]
public ActionResult Unload(jsonObjects jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
var jsonData = JsonConverter(jsonObject);
pdfviewer.ClearCache(jsonData);
return this.Content("Document cache is cleared");
}
[HttpPost]
public ActionResult RenderThumbnailImages(jsonObjects jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
var jsonData = JsonConverter(jsonObject);
object result = pdfviewer.GetThumbnailImages(jsonData);
return Content(JsonConvert.SerializeObject(result));
}
[HttpPost]
public ActionResult Bookmarks(jsonObjects jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
var jsonData = JsonConverter(jsonObject);
object jsonResult = pdfviewer.GetBookmarks(jsonData);
return Content(JsonConvert.SerializeObject(jsonResult));
}
[HttpPost]
public ActionResult RenderAnnotationComments(jsonObjects jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
var jsonData = JsonConverter(jsonObject);
object jsonResult = pdfviewer.GetAnnotationComments(jsonData);
return Content(JsonConvert.SerializeObject(jsonResult));
}
[HttpPost]
public ActionResult Download(jsonObjects jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
var jsonData = JsonConverter(jsonObject);
string documentBase = pdfviewer.GetDocumentAsBase64(jsonData);
return Content(documentBase);
}
[HttpPost]
public ActionResult PrintImages(jsonObjects jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
var jsonData = JsonConverter(jsonObject);
object pageImage = pdfviewer.GetPrintImage(jsonData);
return Content(JsonConvert.SerializeObject(pageImage));
}
private HttpResponseMessage GetPlainText(string pageImage)
{
var responseText = new HttpResponseMessage(HttpStatusCode.OK);
responseText.Content = new StringContent(pageImage, System.Text.Encoding.UTF8, "text/plain");
return responseText;
}
private string GetDocumentPath(string document)
{
string documentPath = string.Empty;
if (!System.IO.File.Exists(document))
{
var path = _hostingEnvironment.ContentRootPath;
if (System.IO.File.Exists(path + "App_Data\\" + document))
documentPath = path + "App_Data\\" + document;
}
else
{
documentPath = document;
}
return documentPath;
}
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
public class jsonObjects
{
public string? document { get; set; }
public string? password { get; set; }
public string? zoomFactor { get; set; }
public string? isFileName { get; set; }
public string? xCoordinate { get; set; }
public string? yCoordinate { get; set; }
public string? pageNumber { get; set; }
public string? documentId { get; set; }
public string? hashId { get; set; }
public string? sizeX { get; set; }
public string? sizeY { get; set; }
public string? startPage { get; set; }
public string? endPage { get; set; }
public string? stampAnnotations { get; set; }
public string? textMarkupAnnotations { get; set; }
public string? stickyNotesAnnotation { get; set; }
public string? shapeAnnotations { get; set; }
public string? measureShapeAnnotations { get; set; }
public string? action { get; set; }
public string? pageStartIndex { get; set; }
public string? pageEndIndex { get; set; }
public string? fileName { get; set; }
public string? elementId { get; set; }
public string? pdfAnnotation { get; set; }
public string? importPageList { get; set; }
public string? uniqueId { get; set; }
public string? data { get; set; }
public string? viewPortWidth { get; set; }
public string? viewportHeight { get; set; }
public string? tilecount { get; set; }
public string? isCompletePageSizeNotReceived { get; set; }
public string? freeTextAnnotation { get; set; }
public string? signatureData { get; set; }
public string? fieldsData { get; set; }
public string? FormDesigner { get; set; }
public string? inkSignatureData { get; set; }
}
}
I modified it a bit to get some of it working atleast,
viewer cshtml contents
<div class="row" id="pdf-contents" style="height: 45vw; width: 100vw; padding: 10px; justify-content: center;" >
@* <ejs-pdfviewer id="pdfviewer" serviceUrl="/api/PDFViewer" documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf" style="height:512px; margin: 50px;"></ejs-pdfviewer> *@
<ejs-pdfviewer id="pdfviewer" style="height:600px" serviceUrl="/PDFViewer" documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf">
</ejs-pdfviewer>
</div>
In our most recent version, we have introduced few properties to the JSON model class. However, the provided code snippet does not include these properties, which may be the root cause of the issue. To assist you further, we have shared a sample with the updated JSON model class in ASP.NET MVC.
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/CoreSample(23.1.39)2068862999.zip
Documentation for JSON Model class: https://ej2.syncfusion.com/aspnetmvc/documentation/pdfviewer/server-actions#jsonobjects-class
Kindly compare our sample and modify your sample accordingly. if you have any concerns on this, Please share the sample in which the issue can be reproduced. This will be helpful for us to analyze further and provide the details.
Hi Karthik sorry to bother you again and again but as I clearly said the model we are going for is MVC not razor pages and it is .NET Core MVC and not .NET Core Razor Page
The sample you've provided doesn't meet our requirement also it doesn't work when I use this url in the documentPath, what I really need is a Controller which does the server side of things and a View.cshtml (.net core mvc not razor page) that just has this
<ejs-pdfviewer id="pdfviewer" style="height:600px" serviceUrl="/PDFViewer" documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf">
Hi Krishna,
Apologies for the
inconvenience. Kindly try the below sample and let us know, if you have any
concerns.
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/CoreSampleMVCBased1081741661.zip