将空手道 html 报告转换为 PDF 格式

问题描述 投票:0回答:1

尝试使用

HTML
JavaScript
Java
或依赖项以编程方式将生成的空手道
Karate
报告从目标文件夹转换为 pdf 格式。

您能帮助我们吗?

javascript java html pdf karate
1个回答
1
投票

在此示例中,单击按钮时,#content div 内的内容将转换为 PDF 并下载:

document.getElementById('download').addEventListener('click', function() {
    var content = document.getElementById('content');
    var opt = {
        margin:       1,
        filename:     'karate-report.pdf',
        image:        { type: 'jpeg', quality: 0.98 },
        html2canvas:  { scale: 2 },
        jsPDF:        { unit: 'mm', format: 'a4', orientation: 'portrait' }
    };

    // Use the html2pdf.js library to save the content as a PDF
    html2pdf().from(content).set(opt).save();
});
body {
    font-family: Arial, sans-serif;
}

#content {
    border: 1px solid #000;
    padding: 10px;
}

button {
    margin-top: 20px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML to PDF Converter</title>
</head>
<body>
    <div id="content">
        <!-- Sample Karate Report HTML content -->
        <h1>Karate Test Report</h1>
        <p>Test Case 1: Passed</p>
        <p>Test Case 2: Failed</p>
        <!-- Add more content as needed -->
    </div>

    <button id="download">Download as PDF</button>

    <!-- Including the html2pdf.js library -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.9.2/html2pdf.bundle.js"></script>
</body>
</html>

https://jsfiddle.net/v5atzq8g/

© www.soinside.com 2019 - 2024. All rights reserved.