html2pdf js not prompting download

Issue

I am using html2pdf.js inside of my projet: https://github.com/eKoopmans/html2pdf.js

My issue, is that I do everything like inside of the tutorial and it does not prompt the download for my div :'(

Here is my code:

$(document).ready(() => {
    $("#download-btn").click(() => {
        generatePDF();
    });
});

function generatePDF() {
    const element = document.getElementById("pdf-content");

    const opt = {
        filename: "methodo.pdf",
        image: { type: "jpeg", quality: 0.85 },
        html2canvas: {
            scale: 5,
            logging: true,
            letterRendering: true,
            useCORS: true,
        },
        jsPDF: {
            unit: "in",
            format: "letter",
            orientation: "portrait",
        },
    };

    // Promise-based usage:
    html2pdf()
        .set(opt)
        .from(element)
        .toPdf()
        .save();
}
.row {
    display: flex;
}

.column {
    flex: 50%;
}

#header {
    display: flex;
    align-items: center;
    height: 150px;
}

#header img {
    height: 100%;
    width: auto;
    margin-left: 15px;
}

#download-btn {
    margin: 0 auto;
    display: block;
}

#pdf-content {
    margin: 5% 5% 15%;
    border-style: solid;
    border-color: lightgray;
    border-width: 2px;
}

#header h2 {
    flex: 75%;
    text-align: center;
}
<script src="https://rawgit.com/eKoopmans/html2pdf/master/dist/html2pdf.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
  <div class="column">
    Questions
  </div>
  <div class="column">
    <div id="pdf-content">
      <div id="header">
        <img src="https://fakeimg.pl/300/?text=image" alt="logo">
        <h2>Fiche Méthodo</h2>
      </div>
    </div>
  <button id="download-btn">Download</button>
  </div>
</div>

There are some console messages that are printed but the pdf does not start to download 🙁

The weird thing, is that if I do something like: html2pdf("Hello World"); It downloads me a pdf with written "hello world" on it. But in my case it does not prompt any download

I hope that someone can help me with that 🙂

Solution

My issue came from the fact that for some reason, the lib does not prompt download when you put a local image inside of the pdf generated.

I had img/image.png inside of my pdf-content that is why it was not working.

Answered By – Lockface

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published