NodeJS Server pdf文档生成问题

问题描述

我正在研究一个API,该API基于EJS模板生成pdf文档,然后将其通过电子邮件发送给某些用户。在本地运行时,一切正常,但是当我的API推送到heroku时,会生成pdf,但出现一些奇怪的格式错误。就像页面实际上是在垂直方向上向下切开一样。

有人知道这可能的原因吗?

我正在使用ejs和html-pdf

这是代码和模板

文档生成功能

ejs.renderFile(path.join(__dirname,'../views/',"offer.ejs"),{offerData},(err,data) => {
                if(err)
                    reject(err)
                else
                {
                    const options = {
                        "height": "11.25in","width": "8.5in","header": {
                            "height": "10mm"
                        },"footer": {
                            "height": "10mm",}                    
                    };
                    //creates the actual pdf doc for sending
                    pdf.create(data,options).toFile(`./offer_${inqID}.pdf`,function(err,res) {
                        if (err) 
                            reject(err)
                        else
                            resolve(res.filename)                        
                      });    
                }
            });

html pdf模板

<!DOCTYPE html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <Meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>Trade-in disclosure Form</title>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.1/css/bootstrap.min.css" integrity="sha384-VCmXjywReHh4PwowAiWNagnWcLhlEJLA5buUprzK8rxFgeH0kww/aWY76TfkUoSX" crossorigin="anonymous">
    <style>
        body{
            font-family: 'Roboto',sans-serif !important;            
        }
        h1{
            text-decoration: underline !important;
        }
        table.center {
            margin-left:auto; 
            margin-right:auto;
          }
        td.rows{
            padding-bottom: 10px;
            text-align: left !important;
        }
        td.question{
            padding-right: 15px;
        }
        span{
            font-weight:bold !important;                        
        }
    </style>
</head>
<body style="border: solid black 2px; margin-left: 10px; margin-right:10px;">
    <header class="text-center">
        <h1 >The Auto broker</h1>
        <h2> Trade-In Vehicle disclosure Form</h2>
        <p style="margin-left: 6em; margin-right: 6em">
            This Form has been filled out by the customer requesting a quote. It contains some basic details to help inform your evaluation of the vehicle.
            If the customer provides false information you have the right to recind or adjust your offer to the customer; otherwise include the value of 
            this vehicle in your total value for the quote. Please ensure to carefully review all the data before providing a quote. Quotes are final and
            cannot be altered once submitted. 
        </p>
    </header>
    <div style="padding-top:20px;">
        <div class="form-container container">
            <div class="row" style="background-color: black;">
                <h3 style="color: aliceblue;" class="ml-2"> Vehicle History</h3>
            </div>
            <table class="center">
                <thead>
                    <tr>
                        <th style="padding-bottom: 20px;"> <h4>Question </h4></th>
                        <th style="padding-bottom: 20px;"> <h4>Response </h4></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class="rows question">Has the Vehicle been in an accident?</td>
                        <td class="rows"> <%= questionnaire.inaccident %> </td>
                    </tr>
                    <tr>
                        <td class="rows question">Have any panels been repainted,repaired <br> or replaced?</td>
                        <td class="rows"> <%= questionnaire.panelrepairs %> </td>
                    </tr>
                    <tr>
                        <td class="rows question">Are you the original owner?</td>
                        <td class="rows"> <%= questionnaire.originalowner %> </td>
                    </tr>
                    <tr>
                        <td class="rows question">Is this an American Vehicle?</td>
                        <td class="rows"> <%= questionnaire.usvehicle %> </td>
                    </tr>
                    <tr>
                        <td class="rows question">Has the Vehicle been registered in any other <br> Province/State?</td>
                        <td class="rows"> <%= questionnaire.outofprovreg %> </td>
                    </tr>
                    <tr>
                        <td class="rows question">Is the odometer faulty,replaced or rolled back? </td>
                        <td class="rows"> <%= questionnaire.faultyodometer %> </td>
                    </tr>
                    <tr>
                        <td class="rows question">Does the vehicle have any lights on the dashboard?</td>
                        <td class="rows"> <%= questionnaire.dashlights %> </td>
                    </tr>
                    <tr>
                        <td class="rows question">Does the vehicle have any factory warranty?</td>
                        <td class="rows"> <%= questionnaire.factorywarranty %> </td>
                    </tr>
                    <tr>
                        <td class="rows question">Does the vehicle have extended warranty?</td>
                        <td class="rows"> <%= questionnaire.extwarrenty %> </td>
                    </tr>
                    <tr>
                        <td class="rows question">Was the vehicle ever used as a daily rental,<br> police vehicle,or taxi/limousine?</td>
                        <td class="rows"> 
                           <% if (questionnaire.rental) { %>
                             Rental <br>
                            <% } %> 
                           
                           <% if (questionnaire.taxilimo) { %>
                            Taxi / Limo <br>
                           <% } %> 
                          
                          <% if (questionnaire.policecar){ %>
                            Police Vehicle <br>
                           <% } %> 
                                        
                         </td>
                    </tr>
                    <tr>
                        <td class="rows question">Does the vehicle require repairs to the following: <br>
                            <span style="margin-left: 10px;">Engine </span> <br> 
                            <span style="margin-left: 10px;">Transmission/Powertrain</span> <br>
                            <span style="margin-left: 10px;">Electrical System </span> <br>
                            <span style="margin-left: 10px;">Air Conditioning System</span>
                        </td>
                        <td class="rows">
                             <%= questionnaire.enginerepair %> <br>
                             <%= questionnaire.transrepair %> <br>
                             <%= questionnaire.electricalrepair %> <br>
                             <%= questionnaire.acrepairs %> <br>
                        </td>
                    </tr>
                    <tr>
                        <td class="rows question">Additional Repair Details</td>
                        <td class="rows"> <%= questionnaire.repairdetails %> </td>
                    </tr>
                    <tr>
                        <td class="rows question">Outstanding Balance</td>
                        <td class="rows"> 
                            <% if (questionnaire.outstandingbalance){ %>
                            <%= questionnaire.outstandingbalance  %>
                           <% } %>  
                        </td>
                    </tr>
                    <tr>
                        <td class="rows question">Total Current Milage</td>
                        <td class="rows"> <%= questionnaire.milage %> K.M </td>
                    </tr>
                    <tr>
                        <td class="rows question">V.I.N</td>
                        <td class="rows"> <%= questionnaire.vin %> </td>
                    </tr>                
                </tbody>
            </table>
        </div>
    </div>
</body>

</html>

正确的图

proper View

破碎的图

Broken Version

解决方法

能够通过添加单个CSS样式规则来解决此问题。

html {
zoom: 0.55;
}

由于html-pdf软件包的默认缩放设置,很有可能需要此设置。此设置以及更多设置也可以通过选项进行配置。但是,我不确定缩放选项是否可以配置为

,

我正面临着同样的挑战。我认为您必须使用内联CSS ...您不能使用引导程序。我面临着同样的问题。但是现在可以使用木偶了