Generate Pdf With JCore

PHOTO EMBED

Sat Oct 08 2022 10:50:59 GMT+0000 (UTC)

Saved by @HristoT #c#

var template = new HtmlTemplateCM();
template.Body = /*read html template from nomenclature or some file*/;

// start replacing the values
template.Body = template.Body.Replace("{{model.MdPatientID}}", patient.MdPatientID.ToString());

var sb = new StringBuilder();
foreach (var dossie in medicalDosies)
{
    var dateTime = DateTime.ParseExact(dossie.FDate, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture);
    sb.AppendLine(@$"
         <tr class=""row"">
               <td class=""col-xs-2"" style=""text-align: left"">{dateTime:dd-MM-yyyy}</td>
               <td class=""col-xs-2"" style=""text-align: left"">{dossie.Abbreviation}</td>
               <td class=""col-xs-6"" style=""text-align: left"">{dossie.IsHasF}</td>
               <td class=""col-xs-2"" style=""text-align: left"">{dossie.DoctorName}</td>
          </tr>");
}

template.Body = template.Body.Replace("{{Details}}", sb.ToString());

// Create the request
var renderReq = new RenderPdfRequest();
renderReq.HtmlHeader = template.Header.RemoveBOM();
renderReq.HtmlBody = template.Body.RemoveBOM();
renderReq.HtmlFooter = template.Footer.RemoveBOM();

var jcore = this.UI.Resolve<JCoreClient>();
var renderResp = jcore.Gateway(renderReq);
if (renderResp.IsFailed)
{
    this.Log($"Проблем с рендерирането на pdf-a", Color.OrangeRed);
    this.Log(renderResp.Print());

}

// Save the generated file if we want to
var render = JsonConvert.DeserializeObject<RenderPdfResponse>(renderResp.JSonValue);
var fileReq = new SaveFileRequest()
{
    FileCode = "03",
    FileContentBase64 = Convert.ToBase64String(render.PdfContent)
};

var fileResp = jcore.Gateway(fileReq);
if (fileResp.IsFailed)
{
    this.Log($"Грешка при записване на файла", Color.OrangeRed);
    this.Log(fileResp.Print());

}

var saveFilePathResp = JsonConvert.DeserializeObject<SaveFileResponse>(fileResp.JSonValue);
var fileID = saveFilePathResp.FileID;

// Save patient record if we want to
var patRecService = this.UI.Resolve<PatientRecordService>();
var rec = new PatientRecordBM
{
    FileID = fileID,
    JRequestID = 1,
    RecStatus = PatientRecordStatusType.Gotov,
    PatientRecShortName = "пациентско досие",
    MongoID = Guid.NewGuid().ToString(),
    KeyID = 1,
    RecordTypeID = (int)PatientRecordType.ResultPdf,
    PatientID = patient.PatientID,
    CollectionName = ResultPdfFile.StaticCollectionName,
    PatientRecDateTime = DateTime.Now,
};

patRecService.Save(rec);
content_copyCOPY