简体   繁体   中英

Html2pf/html2canvas/jsPDF not showing borders using bootstrap modal

I'm not sure which of the required libraries is raising this issue, or if it has something to do with Bootstrap, but I can't get the output PDF to show borders.

I've tried increasing the width of the border but it just gets ignored, not sure what to do here.

Fiddle: https://jsfiddle.net/8q7ce58y/233/

I have tried using .clone() in the modal body, but then it only renders down to what was on the window view.

Code: (it's best to check the fiddle)

<div class="modal" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered modal-lg modal-a4" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Doc Preview</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                      </button>
      </div>
      <div class="modal-body">
        <div class="row m-0">
          <div class="col">
            <p align="center">Text<br></p>
          </div>
        </div>
        <div class="row m-0">
          <div class="col">
            <p><b><span style="font-size: 18px;">Field :</span></b> </p>
            <p>{Field_Name}
            </p>
          </div>
        </div>
        <h3 class="text-center p-2 m-0">Images</h3>
        <div class="row attachment-row m-0">
          <div class="col-6 text-center p-2">
            Image1
          </div>
          <div class="col-6 text-center p-2">
            Image2
          </div>
        </div>
      </div>
      <div class="modal-footer"><button class="ml-auto btn btn-primary ld-ext-right">Download PDF<div class="ld ld-ring ld-cycle running"></div></button></div>
    </div>
  </div>
</div>

Css:

.modal-dialog.modal-a4{
    width: 8.27in !important;
    padding: 0;
}
.modal-a4 .modal-body>*:last-child {
  border-bottom: 10px solid black;
}

.modal-a4 .modal-body>* {
  border-left: 10px solid black;
  border-right: 10px solid black;
}

.modal-a4 .modal-body>*:not(.attachment-row) {
  border-top: 10px solid black;
}

I used html2canvas with jsPDF

function savePdf(){
    // body tag of html
    html2canvas(document.body).then(function(canvas) {
        var jpegUrl = canvas.toDataURL("image/jpeg");
        var doc = new jsPDF('l', 'mm', [318, 210]);
        doc.addImage(jpegUrl, 'JPEG', 10, 10);
        doc.save('sample-file.pdf');
    });
 } 

Libraries

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.min.js"></script>

Download html2canvas: https://html2canvas.hertzen.com/

html2canvas.js
html2canvas.min.js

and call the function by button or input:

<button id="cmd" onclick="savePDF()">generate PDF</button>

html2canvas does not support the css border-left / border-right properties. It only supports border-color, border-width, border-style. See documentation : https://html2canvas.hertzen.com/features

I solved the problem of adding modal, for your reference. GOOD JOB Library jsPdf

    let imageUrl = "url your file image"
    var doc = new jsPDF();
    doc.addImage(imageUrl, 'png', 15, 40, 180, 160);

    var blob = doc.output('blob');
    $('#iframe_view').attr('src', URL.createObjectURL(blob))
    
    

if you want new tab ==>

    window.open(URL.createObjectURL(blob))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM