<script>
    function generateCertifikate(certificateHeader, certificateContent, certificateFooter, findate){
        
        // Format der PDF
        var pdfOrientation = "p";
        var pdfFormat = "a4";

        // Font
        var pdfFont = "helvetica";

        // Seitenränder
        var pdfMarginTop = 25;
        if(certificateHeader['logo_url'].trim() != ""){
            pdfMarginTop = 15;
        }

        var pdfMarginLeft = 25;
        var pdfMarginRight = 25;
        var pdfMarginBottom = 20;

        // Erstellen der PDF-Instanz
        var doc = new jspdf.jsPDF({orientation: pdfOrientation, format: pdfFormat});
        doc.setFont(pdfFont, "normal");

        // Höhe/Breite/Mitte der PDF (Werte jeweils in mm)
        var maxHeight = doc.internal.pageSize.height;
        var maxWidth = doc.internal.pageSize.width;
        var pdfCenter = maxWidth/2;

        // Maximal erlaubte Breite für Inhalt in der PDF (Wert in mm)
        var maxContentWidth = maxWidth - pdfMarginLeft - pdfMarginRight;

        // Aktuelle Position in y-Richtung im Dokument
        var currentHeightPosition = pdfMarginTop;

        var trainingTitle = certificateContent['training_title'];
        if(certificateContent['add_alt_training_title'] == "1"){
            if(certificateContent['alt_training_title'].trim() != ''){
                trainingTitle = certificateContent['alt_training_title'].trim();
            }
        }

        // Binde Logo in Header ein, falls vorhanden
        if(certificateHeader['logo_url'].trim() != ""){
            var logoUrl = certificateHeader['logo_url'];
            var logoProperties = doc.getImageProperties(logoUrl);
            var logoHeight = parseInt(certificateHeader['logo_height']);
            var logoWidth = parseInt(certificateHeader['logo_width']);

            var logoPosition = pdfMarginLeft;
            if(certificateHeader['logo_orientation'] == "left"){
                logoPosition = pdfMarginLeft;
            }
            else if(certificateHeader['logo_orientation'] == "center"){
                logoPosition = (maxContentWidth/2) - (logoWidth/2) + pdfMarginLeft;
            }
            else if(certificateHeader['logo_orientation'] == "right"){
                logoPosition = maxContentWidth - logoWidth + pdfMarginLeft;
            }

            doc.addImage(logoUrl, logoProperties['fileType'], logoPosition, currentHeightPosition, logoWidth, logoHeight, "", "FAST");
            // Füge einen Abstand unterhalb des Bildes hinzu
            currentHeightPosition+= logoHeight + 3*(doc.getFontSize()*0.352778+doc.getLineHeightFactor()*0.352778);
        }

        // Füge Überschrift dem Dokument hinzu, falls vorhanden
        if(certificateContent['titel'].trim() != ""){
            var headlineText = certificateContent['titel'];
            var headlineColor = certificateContent['titel_color'];
            doc.setTextColor(headlineColor);

            doc.setFontSize(35);
            var headlineTextArray = doc.splitTextToSize(headlineText, maxContentWidth);

            for(var headlineTextIndex=0; headlineTextIndex < headlineTextArray.length; headlineTextIndex++){
        
                var headlineTextDim = doc.getTextDimensions(headlineTextArray[headlineTextIndex]);
                var headlineTextPosition = pdfMarginLeft;
                if(certificateContent['titel_orientation'] == "left"){
                    headlineTextPosition = pdfMarginLeft;
                }
                else if(certificateContent['titel_orientation'] == "center"){
                    headlineTextPosition = (maxContentWidth/2) - (headlineTextDim['w']/2) + pdfMarginLeft;
                }
                else if(certificateContent['titel_orientation'] == "right"){
                    headlineTextPosition = maxContentWidth - headlineTextDim['w'] + pdfMarginLeft;
                }

                doc.text(headlineTextArray[headlineTextIndex], headlineTextPosition, currentHeightPosition);
                currentHeightPosition += doc.getFontSize()*0.352778+doc.getLineHeightFactor()*0.352778;
            }
        }

        // Einleitender Text
        doc.setFontSize(15);
        var introductionColor = certificateContent['additional_info_color'];
        doc.setTextColor(introductionColor);

        var altInfoText = "";
        if(certificateContent['add_alt_info_text'] == "1"){
            if(certificateContent['alt_info_text'].trim() != ""){
                altInfoText = certificateContent['alt_info_text'];
                altInfoText = altInfoText.replace(/\[firstname\]/gi, "{{current_user.user_firstname}}").replace(/\[lastname\]/gi, "{{current_user.user_lastname}}").replace(/\[trainingtitle\]/gi, trainingTitle);
            }
        }

        // Füge den Defaultwert für den einleitenden Text ein
        if(altInfoText == ""){
            var introductionText = "{{current_user.user_firstname}}" + " " + "{{current_user.user_lastname}}" + " hat erfolgreich am Training";
            var introductionTextArray = doc.splitTextToSize(introductionText,maxContentWidth);
            for(var introductionTextIndex = 0; introductionTextIndex < introductionTextArray.length; introductionTextIndex++){
                var introductionTextDim = doc.getTextDimensions(introductionTextArray[introductionTextIndex]);
                var introductionTextPosition = pdfMarginLeft;
                if(certificateContent['introduction_orientation'] == "left"){
                    introductionTextPosition = pdfMarginLeft;
                }
                else if(certificateContent['introduction_orientation'] == "center"){
                    introductionTextPosition = (maxContentWidth/2) - (introductionTextDim['w']/2) + pdfMarginLeft;
                }
                else if(certificateContent['introduction_orientation'] == "right"){
                    introductionTextPosition = maxContentWidth - introductionTextDim['w'] + pdfMarginLeft;
                }

                doc.text(introductionTextArray[introductionTextIndex], introductionTextPosition, currentHeightPosition);
                currentHeightPosition += doc.getFontSize()*0.352778+doc.getLineHeightFactor()*0.352778;
            }

            currentHeightPosition += doc.getFontSize()*0.352778+doc.getLineHeightFactor()*0.352778;
        
            // Füge Trainings-Titel dem Dokument hinzu
            var trainingTitleColor = certificateContent['training_title_color'];
            doc.setTextColor(trainingTitleColor);

            doc.setFontSize(25);
            doc.setFont(pdfFont, "bold");
            var trainingTitleArray = doc.splitTextToSize(trainingTitle, maxContentWidth);
            for(var trainingTitleIndex = 0; trainingTitleIndex < trainingTitleArray.length; trainingTitleIndex++){
                var trainingTitleDim = doc.getTextDimensions(trainingTitleArray[trainingTitleIndex]);
                var trainingTitlePosition = pdfMarginLeft;
                if(certificateContent['training_title_orientation'] == "left"){
                    trainingTitlePosition = pdfMarginLeft;
                }
                else if(certificateContent['training_title_orientation'] == "center"){
                    trainingTitlePosition = (maxContentWidth/2) - (trainingTitleDim['w']/2) + pdfMarginLeft;
                }
                else if(certificateContent['training_title_orientation'] == "right"){
                    trainingTitlePosition = maxContentWidth - trainingTitleDim['w'] + pdfMarginLeft;
                }

                doc.text(trainingTitleArray[trainingTitleIndex], trainingTitlePosition, currentHeightPosition);
                currentHeightPosition += doc.getFontSize()*0.352778+doc.getLineHeightFactor()*0.352778;
            }
        
            doc.setFont(pdfFont, "normal");
            doc.setFontSize(15);

            var additionalInformationText = "";

            if(certificateContent['additional_info_text'].trim() != ""){
                additionalInformationText = certificateContent['additional_info_text'];
            }
    
            if(additionalInformationText == ""){
                additionalInformationText += "teilgenommen.";
            }
            else{
                additionalInformationText += " teilgenommen.";
            }
    
            doc.setTextColor(introductionColor);

            var additionalInformationTextArray = doc.splitTextToSize(additionalInformationText, maxContentWidth);
            for(var additionalInformationTextIndex = 0; additionalInformationTextIndex < additionalInformationTextArray.length; additionalInformationTextIndex++){
                var additionalInformationTextDim = doc.getTextDimensions(additionalInformationTextArray[additionalInformationTextIndex]);
                var additionalInformationTextPosition = pdfMarginLeft;
        
                if(certificateContent['introduction_orientation'] == "left"){
                    additionalInformationTextPosition = pdfMarginLeft;
                }
                else if(certificateContent['introduction_orientation'] == "center"){
                    additionalInformationTextPosition = (maxContentWidth/2) - (additionalInformationTextDim['w']/2) + pdfMarginLeft;
                }
                else if(certificateContent['introduction_orientation'] == "right"){
                    additionalInformationTextPosition = maxContentWidth - additionalInformationTextDim['w'] + pdfMarginLeft;
                }

                doc.text(additionalInformationTextArray[additionalInformationTextIndex], additionalInformationTextPosition, currentHeightPosition);
                currentHeightPosition += doc.getFontSize()*0.352778+doc.getLineHeightFactor()*0.352778;
            }

            currentHeightPosition += doc.getFontSize()*0.352778+doc.getLineHeightFactor()*0.352778;
        }
        // Füge alternativen einleitenden Text in Dokument ein
        else{
            var altInfoTextArray = doc.splitTextToSize(altInfoText, maxContentWidth);
        
            for(var altInfoTextIndex = 0; altInfoTextIndex < altInfoTextArray.length; altInfoTextIndex++){
                var altInfoTextDim = doc.getTextDimensions(altInfoTextArray[altInfoTextIndex]);
                var altInfoTextPosition = pdfMarginLeft;
        
                if(certificateContent['introduction_orientation'] == "left"){
                    altInfoTextPosition = pdfMarginLeft;
                }
                else if(certificateContent['introduction_orientation'] == "center"){
                    altInfoTextPosition = (maxContentWidth/2) - (altInfoTextDim['w']/2) + pdfMarginLeft;
                }
                else if(certificateContent['introduction_orientation'] == "right"){
                    altInfoTextPosition = maxContentWidth - altInfoTextDim['w'] + pdfMarginLeft;
                }

                doc.text(altInfoTextArray[altInfoTextIndex], altInfoTextPosition, currentHeightPosition);
                currentHeightPosition += doc.getFontSize()*0.352778+doc.getLineHeightFactor()*0.352778;
            }

            currentHeightPosition += doc.getFontSize()*0.352778+doc.getLineHeightFactor()*0.352778;
        }

        // Füge optionale Beschreibung des Trainings dem Dokument hinzu
        
        if(certificateContent['additional_traininginfo_text'].trim() != ""){
            var additionalTrainingInfoText = certificateContent['additional_traininginfo_text'];
            var additionalTraininginfoColor = certificateContent['additional_traininginfo_color'];
            doc.setTextColor(additionalTraininginfoColor);

            doc.setFontSize(15);
            var additionalTrainingInfoTextArray = doc.splitTextToSize(additionalTrainingInfoText, maxContentWidth);

            for(var additionalTrainingInfoTextIndex = 0; additionalTrainingInfoTextIndex < additionalTrainingInfoTextArray.length; additionalTrainingInfoTextIndex++){
                var additionalTrainingInfoTextDim = doc.getTextDimensions(additionalTrainingInfoTextArray[additionalTrainingInfoTextIndex]);
                var additionalTrainingInfoTextPosition = pdfMarginLeft;
                if(certificateContent['additional_traininginfo_orientation'] == "left"){
                    additionalTrainingInfoTextPosition = pdfMarginLeft;
                }
                else if(certificateContent['additional_traininginfo_orientation'] == "center"){
                    additionalTrainingInfoTextPosition = (maxContentWidth/2) - (additionalTrainingInfoTextDim['w']/2) + pdfMarginLeft;
                }
                else if(certificateContent['additional_traininginfo_orientation'] == "right"){
                    additionalTrainingInfoTextPosition = maxContentWidth - additionalTrainingInfoTextDim['w'] + pdfMarginLeft;
                }

                doc.text(additionalTrainingInfoTextArray[additionalTrainingInfoTextIndex], additionalTrainingInfoTextPosition, currentHeightPosition);
                currentHeightPosition += doc.getFontSize()*0.352778+doc.getLineHeightFactor()*0.352778;
            }
            currentHeightPosition += doc.getFontSize()*0.352778+doc.getLineHeightFactor()*0.352778;
        }

        // Schlussformel
        doc.setFontSize(15);
        var closingDate = new Date(findate);
        var closingDateDay = "";
        if(closingDate.getDate()>=10){
            closingDateDay = closingDate.getDate();
        }
        else{
            closingDateDay = "0"+closingDate.getDate();
        }

        var closingDateMonth = "";
        if((closingDate.getMonth()+1)>=10){
            closingDateMonth = (closingDate.getMonth()+1);
        }
        else{
            closingDateMonth = "0"+(closingDate.getMonth()+1);
        }
        
        var closingLocation = certificateContent['closing_location'];

    
        var closingLocationDate = closingLocation + ", " + closingDateDay + "." + closingDateMonth + "." + closingDate.getFullYear();
    
        var closingColor = certificateContent['closing_color'];
        doc.setTextColor(closingColor);

        var closingLocationDateArray = doc.splitTextToSize(closingLocationDate, maxContentWidth);
        for(var closingLocationDateIndex = 0; closingLocationDateIndex < closingLocationDateArray.length; closingLocationDateIndex++){
            var closingLocationDateDim = doc.getTextDimensions(closingLocationDateArray[closingLocationDateIndex]);
            var closingLocationDatePosition = pdfMarginLeft;
            if(certificateContent['closing_orientation'] == "left"){
                closingLocationDatePosition = pdfMarginLeft;
            }
            else if(certificateContent['closing_orientation'] == "center"){
                closingLocationDatePosition = (maxContentWidth/2) - (closingLocationDateDim['w']/2) + pdfMarginLeft;
            }
            else if(certificateContent['closing_orientation'] == "right"){
                closingLocationDatePosition = maxContentWidth - closingLocationDateDim['w'] + pdfMarginLeft;
            }

            doc.text(closingLocationDateArray[closingLocationDateIndex], closingLocationDatePosition, currentHeightPosition);
            currentHeightPosition += doc.getFontSize()*0.352778+doc.getLineHeightFactor()*0.352778;
        }

        doc.setFontSize(10);
        var closingPersons = certificateContent['closing_persons'];
        var closingPersonsArray = doc.splitTextToSize(closingPersons, maxContentWidth);
        for(var closingPersonsIndex = 0; closingPersonsIndex < closingPersonsArray.length; closingPersonsIndex++){
            var closingPersonsDim = doc.getTextDimensions(closingPersonsArray[closingPersonsIndex]);
            var closingPersonsPosition = pdfMarginLeft;
            if(certificateContent['closing_orientation'] == "left"){
                closingPersonsPosition = pdfMarginLeft;
            }
            else if(certificateContent['closing_orientation'] == "center"){
                closingPersonsPosition = (maxContentWidth/2) - (closingPersonsDim['w']/2) + pdfMarginLeft;
            }
            else if(certificateContent['closing_orientation'] == "right"){
                closingPersonsPosition = maxContentWidth - closingPersonsDim['w'] + pdfMarginLeft;
            }

            doc.text(closingPersonsArray[closingPersonsIndex], closingPersonsPosition, currentHeightPosition);
            currentHeightPosition += doc.getFontSize()*0.352778+doc.getLineHeightFactor()*0.352778;
        }

        if(certificateContent['closing_img_url'].trim() != ""){
            var closingImgUrl = certificateContent['closing_img_url'];
            var closingImgProperties = doc.getImageProperties(closingImgUrl);
            var closingImgHeight = parseInt(certificateContent['closing_img_height']);
            var closingImgWidth = parseInt(certificateContent['closing_img_width']);
        
            var closingImgPosition = pdfMarginLeft;
            if(certificateContent['closing_orientation'] == "left"){
                closingImgPosition = pdfMarginLeft;
            }
            else if(certificateContent['closing_orientation'] == "center"){
                closingImgPosition = (maxContentWidth/2) - (closingImgWidth/2) + pdfMarginLeft;
            }
            else if(certificateContent['closing_orientation'] == "right"){
                closingImgPosition = maxContentWidth - closingImgWidth + pdfMarginLeft;
            }

            doc.addImage(closingImgUrl, closingImgProperties['fileType'], closingImgPosition, currentHeightPosition, closingImgWidth, closingImgHeight, "", "FAST");
            // Füge einen Abstand unterhalb des Bildes hinzu
            currentHeightPosition+= logoHeight + 3*(doc.getFontSize()*0.352778+doc.getLineHeightFactor()*0.352778);
        }
        
        // Optionaler Footer
        if(certificateFooter['footer_text'].trim() != ""){
            var footerText = certificateFooter['footer_text'];
            doc.setFontSize(10);
            var footerColor = certificateFooter['footer_color'];
            doc.setTextColor(footerColor);
            var footerTextArray = doc.splitTextToSize(footerText, maxContentWidth);

             var footerHeightPosition = maxHeight - pdfMarginBottom;

            for(var footerTextIndex = (footerTextArray.length-1); footerTextIndex >= 0; footerTextIndex--){
                doc.text(footerTextArray[footerTextIndex], pdfMarginLeft, footerHeightPosition);
                footerHeightPosition -= doc.getFontSize()*0.352778+doc.getLineHeightFactor()*0.352778;
            }
        }

        var currentDate = new Date();
        var currentDateDay = "";
        if(currentDate.getDate()>=10){
            currentDateDay += currentDate.getDate();
        }
        else{
            currentDateDay = "0"+currentDate.getDate();
        }

        var currentDateMonth = "";
        if((currentDate.getMonth()+1)>=10){
            currentDateMonth += (currentDate.getMonth()+1);
        }
        else{
            currentDateMonth = "0"+(currentDate.getMonth()+1);
        }
        var currentDateHour = "";
        if(currentDate.getHours()>=10){
            currentDateHour += currentDate.getHours();
        }
        else{
            currentDateHour = "0"+currentDate.getHours();
        }

        var currentDateMinutes = "";
        if(currentDate.getMinutes()>=10){
            currentDateMinutes += currentDate.getMinutes();
        }
        else{
            currentDateMinutes = "0"+currentDate.getMinutes();
        }

        // Ergänze Kennzeichnung, sofern ausgewählt
        if(certificateFooter['add_userid'] == "1"){
            
            // Ergänze Validierungscode, sofern ausgewählt
            if(certificateFooter['add_validationcode'] == "1"){
                var codeTime = currentDateHour+currentDateMinutes;
                var trainingId = certificateContent['training_id'];
                var validationCode = parseInt('{{current_user.ID}}')*parseInt(trainingId)+parseInt(codeTime);
                var validationCodeText = "Validierungscode: "+validationCode;
                doc.setTextColor('#000000');
                doc.setFontSize(8);
                doc.text(validationCodeText, 5, maxHeight-5-2*doc.getFontSize()*0.352778+doc.getLineHeightFactor()*0.352778); 
            }

            var identificationText = "Wurde vom Teilnehmenden mit der ID {{current_user.ID}} am " + currentDateDay + "." + currentDateMonth + "." + currentDate.getFullYear() + " um " + currentDateHour + ":" + currentDateMinutes + " Uhr erstellt.";
            doc.setTextColor('#000000');
            doc.setFontSize(8);
            doc.text(identificationText, 5, maxHeight-5); 
        }

        doc.save("Teilnahmezertifikat");
    }
</script>