I have successfully saved a PDF created from a word doc template utilizing mail merge events in an asp.net core razor application
I would prefer not to save the file locally and upon rendering the PDF from the word file, view it on my webpage to give the user the ability to view the details in the pdf and print if necessary. I'm stuck on returning the pdf to the Viewer and loading it after creating it. Worst case, i would like to save over previously created pdf's
Thanks
the method in my controller:Details.cshtml.cs
public async Task<IActionResult> OnPostGeneratePDFAsync()
{
Box = await _context.Box
.Include(b => b.Location)
.Include(b => b.Part).FirstOrDefaultAsync(m => m.BoxId == 75);
string dataPath = _hostingEnvironment.WebRootPath + @"/images/";
FileStream docStream = new FileStream(dataPath + "TTTT.docx", FileMode.Open, FileAccess.Read);
WordDocument document = new WordDocument(docStream, Syncfusion.DocIO.FormatType.Automatic);
string[] fieldNames = new string[] { "loadNumber", "loadDate" };
string[] fieldValues = new string[] { "test load", "test/date" };
document.MailMerge.Execute(fieldNames, fieldValues);
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
stream.Position = 0;
// Creates a new instance of DocIORenderer class.
DocIORenderer render = new DocIORenderer();
// Converts Word document into PDF document.
PdfDocument pdf = render.ConvertToPDF(document);
MemoryStream memoryStream = new MemoryStream();
// Save the PDF document.
pdf.Save(memoryStream);
render.Dispose();
pdf.Close();
document.Close();
memoryStream.Position = 0;
return File(memoryStream, "application/pdf", "WordToPDF.pdf");
}
Details.cshtml
@page "{id}"
@model Summit.Pages.BackOffice.Boxes.DetailsModel
@{
ViewData["Title"] = "Details";
}
<div class="container">
<form id="pdfCreate" method="post" asp-page-handler="GeneratePDF" style="display:inline"><h2 style="display:inline">Box @Html.DisplayFor(model => model.Box.BoxId) Details:</h2><input type="submit" class="btn btn-primary btn-xs" value="Box Label" /></form>
@section ControlsSection{
<div class="control-section">
<ejs-pdfviewer id="pdfviewer" style="height:641px;"></ejs-pdfviewer>
</div>
}
<script type="text/javascript">
window.onload = function () {
var pdfViewer = document.getElementById('pdfviewer').ej2_instances[0];
pdfViewer.serviceUrl = window.baseurl + 'api/PdfViewer';
pdfViewer.load("PDF Succinctly.pdf", null);
}
</script>