Search code examples
javascriptvue.jsjspdf

how to add page break on jspdf html


I want to add a page break in jspdf, how to add page break automatically when page will be printed, i use jspdf plugin in vue 3, is it possible to add page break using those plugins?

enter image description here

my code :

<template>
  <div class="parent">
    <div
      class="pdf"
      id="pdf"
      style="
        display: block;
        width: 450px;
        padding: 70px;
        padding-top: 18px;
        padding-left: 16px;
      "
    >
      <div
        v-for="(index, arr) of Array(30)"
        style="
          display: inline-block;
          margin: 15px 0;
          margin-left: 7px;
          text-align: center;
        "
      >
        <div style="font-size: 8.25px">SCS111022021235</div>
        <div style="padding: 4.5px; border: 1px dashed black">
          <img :src="qrCodeUrl" style="width: calc(100px * 0.55)" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import QRCode from "qrcode";
import { jsPDF } from "jspdf";

const qrCodeUrl = ref("");

const doc = new jsPDF("portrait", "px", "a4");

QRCode.toDataURL("asdasd", { margin: 0 }).then(url => {
  qrCodeUrl.value = url;
});
onMounted(() => {
  doc.html(document.getElementById("pdf")!, {
    callback: doc => {
      // doc.save();
      window.open(doc.output("bloburl"), "_blank");
    },
    x: 10,
    y: 10
  });
});
</script>

Solution

  • You need set autoPaging to 'text' in .html method

    Example:

      doc.html(document.getElementById("pdf")!, {
        callback: doc => {
          // doc.save();
          window.open(doc.output("bloburl"), "_blank");
        },
        x: 10,
        y: 10,
        autoPaging: 'text'
      });
    

    It helped me. I found solution here: https://stackoverflow.com/a/70211059/7027059