public static byte[] ConvertHtmlSringToPDF(FirmInstitution firmInstitutionSDG, string cgpName, Data.Model.Document document, string destinationPathFileName, bool forceDownload, out int pageCount, string logoUrl = "")
        {            
            Byte[] documentBytes = null;
            pageCount = 0;
            try
            {
                var firmSDG = GetFirmSDG();
                var languageData = PageLanguageHelper.GetLanguageContent("User", "Convention");

                DocumentModel documentModel = GetDocumentModel(firmInstitutionSDG.idFirmInstitution, !string.IsNullOrEmpty(document.DocumentType) ? document.DocumentType : "");
                if(documentModel != null)
                {
                    string strHtmlMapped = null;
                    string strFooter = null;

                    var model = EntityToModel(documentModel);
                    strHtmlMapped = AppendIntoBodyHtml(model.HtmlTemplate);

                    strHtmlMapped = strHtmlMapped.Replace("[sdg_lieu_fait_a]", firmSDG.FirmCity);
                    strHtmlMapped = strHtmlMapped.Replace("[sdg_attention]", "Service administratif");

                    using (UpsilabEntities context = new UpsilabEntities())
                    {
                        var firmInstitution = context.FirmInstitution.Where(f => f.idFirmInstitution == firmInstitutionSDG.idFirmInstitution).FirstOrDefault();


                        strHtmlMapped = MapSignatoryKeys(context, document.idSignatureTransaction, strHtmlMapped); 

                        strFooter = firmInstitution.DocumentFooterContent.ToString();
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_NOM + "]",
                            firmInstitution.FirmInstitutionName);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_DIRIGEANT_NOM + "]",
                            firmInstitution.LeaderName);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_DIRIGEANT_PRENOM + "]",
                            firmInstitution.LeaderFirstName);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_DIRIGEANT_CIVILITE + "]",
                            firmInstitution.User.Civility);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_DIRIGEANT_FONCTION + "]",
                            firmInstitution.LeaderFonction);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_DIRIGEANT_DATE_NAIS + "]",
                            firmInstitution.LeaderBirthDay.ToString());
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_DIRIGEANT_LIEU_NAIS + "]",
                            firmInstitution.LeaderPlaceOfBirth);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_DIRIGEANT_FORMATION + "]",
                            firmInstitution.LeaderFormation);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_DIRIGEANT_CURSUS + "]",
                            firmInstitution.LeaderCursusProfessional);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_ADRESSE + "]",
                            firmInstitution.FirmStreet);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_CODE_POSTAL + "]",
                            firmInstitution.FirmPostCode);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_VILLE + "]",
                            firmInstitution.FirmCity);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_TEL_PROF + "]",
                            firmInstitution.FirmPhone);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_TEL_PORTABLE + "]",
                            firmInstitution.MobilePhone);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_FAX + "]", firmInstitution.Fax);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_EMAIL + "]",
                            firmInstitution.E_mail);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_WEB + "]",
                            firmInstitution.Website);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_APE + "]",
                            firmInstitution.NAF_APE);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_NAF + "]", firmInstitution.NAF);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_CAPITAL + "]",
                            firmInstitution.ShareCapital);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.DATE_FAIT_A + "]",
                            DateTime.Now.ToString("d"));
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_NUM_AGREMENT + "]",
                            firmInstitution.AgreementNumber);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_NUM_SOCIETE + "]",
                            firmInstitution.RCS);
                        strHtmlMapped = strHtmlMapped.Replace("[" + FieldsEditorBL.ETAB_NUM_ASSURANCE + "]",
                            firmInstitution.NumeroAssuranceQBE);

                        strHtmlMapped = strHtmlMapped.Replace("[logo]", logoUrl + "NextStatge.jpg");

                        if (forceDownload == true)
                        {
                            strHtmlMapped = strHtmlMapped.Replace("[readApprovedLabel]", string.Format("{0}.", languageData.GetContent("Read_approved_signature_label")));
                            strHtmlMapped = strHtmlMapped.Replace("[dateApprovedLabel]", string.Format("{0} : ", languageData.GetContent("Date_approved_signature_label")));
                        }
                        else
                        {
                            strHtmlMapped = strHtmlMapped.Replace("[readApprovedLabel]", "");
                            strHtmlMapped = strHtmlMapped.Replace("[dateApprovedLabel]", "");
                        }


                        var valDico = new Dictionary<string, string>();
                        var fieldDico = new Dictionary<string, FieldValueModel>();
                        //Get all NameKey exist
                        var nameKeys = FieldValuesBL.GetAllNameKeyForMapping(documentModel.IdDocumentModel);
                        var listNameKey = nameKeys.ToList();

                        //Replace all NameKey value to empty                        
                        foreach (var item in listNameKey)
                        {
                            valDico[item.NameKey] = string.Empty;
                            fieldDico[item.NameKey] = item;
                        }


                        //Get all NameKey which has value
                        var dico = FieldValuesBL.GetForMapping(document.idDocument);

                        foreach (var item in dico)
                        {
                            //Replace NameKey value with his right value
                            valDico[item.Key] = item.Value;
                        }
                        
                        foreach (var fv in valDico)
                        {
                            var value = fv.Value;
                            var nk = fieldDico[fv.Key] ?? new FieldValueModel() { Value = string.Empty };
                            var type = (nk != null && !string.IsNullOrEmpty(nk.Value)) ? nk.Value : "";

                            switch (type)
                            {
                                case "radio":
                                case "radiogroup":
                                //case "civilite":
                                    var rad_pattern = string.Format("ckvalue=\"{0}_{1}\"", fv.Key, value);
                                    var rad_replace = "checked";
                                    strHtmlMapped = strHtmlMapped.Replace(rad_pattern, rad_replace);
                                    break;
                                case "civilite":
                                    if(!string.IsNullOrEmpty(value)) 
                                    {
                                        var pattern = value.ToLower();
                                        value = languageData.GetContent(string.Format("civilite_{0}", value.ToLower()));
                                    }
                                    strHtmlMapped = strHtmlMapped.Replace("[" + fv.Key + "]", !string.IsNullOrEmpty(value) ? value : string.Empty);
                                    break;
                                case "checkbox":
                                    var cbx_pattern = string.Format("value=\"[{0}]\"", fv.Key);
                                    var cbx_replace = (value == "True") ? "checked" : "";
                                    strHtmlMapped = strHtmlMapped.Replace(cbx_pattern, cbx_replace);
                                    break;

                                case "numeric":
                                    if (!String.IsNullOrEmpty(value))
                                    {
                                        decimal d = 0;
                                        Decimal.TryParse(value.Replace('.', ','), out d);
                                        if (d > 0)
                                        {
                                            var f = new NumberFormatInfo { NumberGroupSeparator = " ", CurrencyDecimalDigits = 2 }; //TODO cultureInfo
                                            value = d.ToString("n", f);
                                        }
                                    }
                                    strHtmlMapped = strHtmlMapped.Replace("[" + fv.Key + "]", !string.IsNullOrEmpty(value) ? value : string.Empty);
                                    break;

                                case "ibanfield":
                                    string[] ibanwords = value.Split(' ');
                                    for (int i = 0; i < ibanwords.Length; i++)
                                    {
                                        var iban_pattern = string.Format("ibanvalue=\"{0}_{1}\"", fv.Key, i);
                                        var iban_replace = string.Format("value=\"{0}\"", ibanwords[i]);  //ibanwords[i];
                                        strHtmlMapped = strHtmlMapped.Replace(iban_pattern, iban_replace);
                                    }
                                    break;
                                case "datetime":
                                    if (!string.IsNullOrEmpty(value))
                                    {
                                        var dt = DateTime.MinValue;
                                        DateTime.TryParse(value, CultureInfo.CreateSpecificCulture("Fr"), DateTimeStyles.None, out dt);
                                        if (dt == DateTime.MinValue)
                                        {
                                            DateTime.TryParse(value, CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.None, out dt);
                                        }
                                        if (dt != DateTime.MinValue)
                                        {
                                            value = dt.ToString("dd/MM/yyyy");
                                        }
                                    }
                                    strHtmlMapped = strHtmlMapped.Replace("[" + fv.Key + "]", !string.IsNullOrEmpty(value) ? value : string.Empty);
                                    break;
                                default:
                                    strHtmlMapped = strHtmlMapped.Replace("[" + fv.Key + "]", !string.IsNullOrEmpty(value) ? value : string.Empty);
                                    break;
                            }

                        }


                }

                    if (!forceDownload)
                    {
                        // save the PDF bytes in a file on disk
                        string outFilePath = System.IO.Path.Combine(destinationPathFileName);
                        ExpertPDFHelper expertPdfEngine = new ExpertPDFHelper();
                        ExpertPdf.HtmlToPdf.PdfDocument.Document pdfDocument = expertPdfEngine.GeneratePdfDocumentUsingComponent(strHtmlMapped, cgpName, strFooter, true);
                        pageCount = pdfDocument.Pages.Count;
                        pdfDocument.Save(outFilePath);

                    }
                    else
                    {
                         ExpertPDFHelper expertPdfEngine = new ExpertPDFHelper();
                         documentBytes = expertPdfEngine.GeneratePdfBytesUsingComponent(strHtmlMapped, cgpName, strFooter, true);  
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Erreur lors de la génération du template HTML en PDF : {0}", ex.Message));
            }

            return documentBytes;
        }
        public static int ConvertHtmlSringBdsProductToPdf(string name, Guid idDocumentModel, string destinationPath, Guid idUser, FirmInstitutionViewModel firmCGP, Guid? idDocument, bool forceDownload, string logoUrl = "")
        {
            int pageCount = 0;
            Byte[] documentBytes = null;
            try
            {

                DocumentModel documentModel = GetDocumentModelFromId(idDocumentModel);
                if (documentModel != null && idDocument.HasValue)
                {
                    string strHtmlMapped = null;
                    string strFooter = null;

                    var model = EntityToModel(documentModel);
                    strHtmlMapped = AppendIntoBodyHtml(model.HtmlTemplate);
                    //strHtmlMapped = AppendIntoBodyHtml(documentModel.HtmlTemplate);

                    ExpertPDFHelper expertPdfEngine = new ExpertPDFHelper();
                    documentBytes = expertPdfEngine.GeneratePdfBytesUsingComponent(ParserHtmlMethod(documentModel, idDocument.Value, strHtmlMapped, logoUrl, true, firmCGP), name, strFooter, false);

                    FoxitPDFGenerator pdfGen = new FoxitPDFGenerator();

                    string docTitle = string.Empty; //TODO : multilingue
                    string ownerName = name;

                    pdfGen.GenerateMergeByteDocument(documentModel.IdDocumentModel, documentBytes, destinationPath, "", forceDownload, out pageCount, Upsilab.Business.Document.ElectronicSafeDocumentBL.DocumentType.SF.ToString(), "center", docTitle, ownerName,false,false);
                     
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Erreur lors de la génération du template HTML en PDF : {0}", ex.Message));
            }

            return pageCount;
        }