We have implemented the PdfViewer using the Syncfusion EJ2 javascript component(@syncfusion/ej2). Also, the web service is implemented using .net core API. Now we are facing caching issues while taking the PDF. We have 5 pdf reports. When I took the first one it's showing as expected, after that, I will take the remaining ones it will still show the first loaded PDF. We have also implemented the unload method for clearing the memory cache and it's working as expected. When we clear the "site data" , it is working as expected for some time. The issue exists when resource usage is exceeded.
[HttpPost]
[Route("api/[controller]/Load")]
public async Task Upload([FromBody] Dictionary jsonObject)
{
try
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
MemoryStream stream=null;
object jsonResult = new object();
if (jsonObject != null && jsonObject.ContainsKey("document"))
{
if (bool.Parse(jsonObject["isFileName"]))
{
var fileName = jsonObject["document"];
var storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(_appSettings.AzureFileServiceSettings.AccountName, _appSettings.AzureFileServiceSettings.AccessKey);
CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
CloudFileShare share = fileClient.GetShareReference("xxxxxx");
if (await share.ExistsAsync(null, null))
{
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
CloudFile cfile = rootDir.GetFileReference(fileName);
if (await cfile.ExistsAsync(null, null))
{
stream = new MemoryStream();
await cfile.DownloadToStreamAsync(stream, null, null, null);
}
}
}
else
{
byte[] bytes = Convert.FromBase64String(jsonObject["document"]);
stream = new MemoryStream(bytes);
}
}
if (stream == null)
return this.Content(jsonObject["document"] + " is not found");
jsonResult = pdfviewer.Load(stream, jsonObject);
return Content(JsonConvert.SerializeObject(jsonResult));
}
catch (Exception ex)
{
LogException(ex, "AccountInvoiceHeaderController", "Upload([FromBody] Dictionary jsonObject)");
throw ex;
}
}
[AcceptVerbs("Post")]
[HttpPost]
[Route("api/[controller]/RenderPdfPages")]
public async Task RenderPdfPages([FromBody] Dictionary jsonObject)
{
try
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
object jsonResult = pdfviewer.GetPage(jsonObject);
return Content(JsonConvert.SerializeObject(jsonResult));
}
catch (Exception ex)
{
LogException(ex, "AccountInvoiceHeaderController", "RenderPdfPages([FromBody] Dictionary jsonObject)");
throw ex;
}
}
[AcceptVerbs("Post")]
[HttpPost]
[Route("api/[controller]/Download")]
public async Task Download([FromBody] Dictionary jsonObject)
{
try
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject);
return Content(documentBase);
}
catch (Exception ex)
{
LogException(ex, "PaymentController", "Download([FromBody] Dictionary jsonObject)");
throw ex;
}
}
[AcceptVerbs("Post")]
[HttpPost]
[Route("api/[controller]/PrintImages")]
public async Task PrintImages([FromBody] Dictionary jsonObject)
{
try
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
object pageImage = pdfviewer.GetPrintImage(jsonObject);
return Content(JsonConvert.SerializeObject(pageImage));
}
catch (Exception ex)
{
LogException(ex, "AccountInvoiceHeaderController", "PrintImages([FromBody] Dictionary jsonObject)");
throw ex;
}
}
[AcceptVerbs("Post")]
[HttpPost]
[Route("api/[controller]/Unload")]
public IActionResult Unload([FromBody] Dictionary jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
pdfviewer.ClearCache(jsonObject);
return this.Content("Document cache is cleared");
}
export class AccountInvoice {
route: IRoute
logger = LogManager.getLogger('AccountInvoice');
pdfViewer: PdfViewer;
constructor(private router: AppRouter) {
}
async activate(info: IRoute) {
this.route = info;
this.pdfViewer = new PdfViewer();
}
async attached() {
this.pdfViewer.serviceUrl = window.location.protocol + "//" + window.location.host + '/api/xxxxxxxx';
this.pdfViewer.documentPath = this.route.invoiceFileName;
this.pdfViewer.toolbarSettings = { showTooltip: true, toolbarItems: ["PageNavigationTool", "SearchOption", "PrintOption", "DownloadOption"] };
this.pdfViewer.enableThumbnail = false;
this.pdfViewer.enableBookmark = false;
this.pdfViewer.enableAnnotation = false;
//PDF Viewer control rendering starts
this.pdfViewer.appendTo('#pdfViewer');
}
async deactivate() {
this.pdfViewer.destroy();
}