I have a problem creating pdf.
scenario: I create pdf from html. I need to dynamically create separate HTML for my footer and header areas.
Problem: After receiving my main HTML content according to my draft via ajax request, I download the file on the server for the footer. I open the file I downloaded and change the text in it with Replace. Then I upload it to the folder I specified with a new name. After the file is uploaded to the folder, it refreshes the page and my pdf creation process is interrupted.
I will be giving you my js codes and methods in the Controller below. What solution should I implement?
The line where the problem occurs: await System.IO.File.WriteAllTextAsync(footerFilePath, footerContent);
js codes:
function loadTemplateContent(pdfTemplateID) {
var fDocumentGUID = $('#documentGUID').val();
$.ajax({
url: '/_Ajax/GetPDFTemplateContent',
data: { pdfTemplateID: pdfTemplateID, fDocumentGUID: fDocumentGUID },
success: function (data) {
originalContent = data;
CKEDITOR.instances['TemplateContent'].setData(data);
//footerContent = data.footer;
}
});
}
method:
[HttpGet]
public async Task<IActionResult> GetPDFTemplateContent(int pdfTemplateID, Guid? fDocumentGUID)
{
var template = await _serviceManager.PDFTemplates.GetByIdAsync(pdfTemplateID);
if (template == null)
{
return Json(string.Empty);
//return Json(new { header = string.Empty, content = string.Empty, footer = string.Empty });
}
string content = template.TemplateContent;
string footerContent = string.Empty;
if (fDocumentGUID.HasValue)
{
var fDocument = await _serviceManager.FDocuments.GetEntityAsync(a => a.GUID == fDocumentGUID.Value);
if (fDocument != null)
{
var selectedDataVW = await _serviceManager.VW_FDocuments.GetEntityAsync(a => a.GUID == fDocumentGUID.Value);
var customerAdress = _serviceManager.Customers.GetValue(a => a.GUID == selectedDataVW.CustomerGUID, "Adress");
var responsibleMember = await _serviceManager.VW_Members.GetEntityAsync(a => a.MemberID == selectedDataVW.ResponsibleMemberID);
var obligatoryMemberNames = await _MemberDataService.GetMemberIDsWithNamesAndTypes(fDocument.ObligatoryMemberIDs, false);
var (infoMemberNames, infoMemberTypeNames) = await _MemberDataService.GetMemberIDsWithNamesAndTypes(fDocument.InfoMemberIDs, true);
content = content.Replace("$$FDocNo$$", selectedDataVW.FDocNo);
content = content.Replace("$$FDocDate$$", selectedDataVW.FDocDate.ToShortDateString());
content = content.Replace("$$CustomerName$$", selectedDataVW.CustomerName);
content = content.Replace("$$CustomerAdress$$", customerAdress);
content = content.Replace("$$ResponsibleMember$$", responsibleMember.NameSurname);
content = content.Replace("$$ResponsibleMemberTitle$$", responsibleMember.MemberTypeName);
content = content.Replace("$$InfoMember$$", infoMemberNames);
content = content.Replace("$$InfoMemberTitle$$", infoMemberTypeNames);
content = content.Replace("$$ToplantiyaKatilanlar$$", obligatoryMemberNames.MemberNameSurname);
}
}
//footer
string footerUrl = $"https://tkba.tdub.org.tr/lib/Templates/{template.FooterText}";
footerContent = await _PdfService.DownloadContent(footerUrl);
string formattedDate = DateTime.Now.ToString("yyyyMMddHHmmssfff");
var allowedFooters = new List<string> { "Footer_1.html", "Footer_3.html" };
if (allowedFooters.Contains(template.FooterText))
{
var FooterFileName = $"{template.FooterText}-{formattedDate}.html";
string footerFilePath = Path.Combine(_env.WebRootPath, "images/Uploads", FooterFileName);
var fDocument = await _serviceManager.FDocuments.GetEntityAsync(a => a.GUID == fDocumentGUID.Value);
var (infoMemberNames, infoMemberTypeNames) = await _MemberDataService.GetMemberIDsWithNamesAndTypes(fDocument.InfoMemberIDs, true);
footerContent = footerContent.Replace("$$InfoMember$$", infoMemberNames);
footerContent = footerContent.Replace("$$InfoMemberTitle$$", infoMemberTypeNames);
await System.IO.File.WriteAllTextAsync(footerFilePath, footerContent);
//footerContent = FooterFileName;
}
//footer
//return Json(new { success = true, content = content, footer = footerContent });
return Json(content);
}
When I select one of the drafts that come with the select list, the content of the relevant draft will be loaded, then I will download the footer field of the draft from the url on the server and replace the relevant fields. Afterwards, I want to upload the current footer to the folder. After uploading to the folder, I want to get the name of the current footer here and continue my pdf creation method.
my problem; After creating and uploading the file, it refreshes the page. In this way, I cannot create my html content as pdf.