How add margin in jsPdf?

44 Views Asked by At

I need to save a block from a page to PDF. Due to the fact that I am using Vue, I was unable to correctly use JSPDF to save this block.

Using the dom-to-image library, I converted the block into an image and then output it via JSPDF:

const domImg = await domtoimage.toPng(page)

    const pdf = new jsPDF('p', 'mm', 'a4')
    const height = page.offsetHeight
    const heightImgBlock = 353
    const heightA4px = 2480

    const addPage = (currentHeight = 5, heightBlock = height) => {
        if (heightBlock < 0) return
        if (currentHeight < heightImgBlock) {
            const actualHeight = currentHeight - 295
            const heightBlockActual = heightBlock - heightA4px
            pdf.addPage()
            pdf.addImage(domImg, 'JPEG', 5, actualHeight, 0, 0)
            addPage(actualHeight, heightBlockActual)
        }
    }

    if (height > heightImgBlock) {
        pdf.addImage(domImg, 'JPEG', 5, 5, 0, 0)
        addPage()
    } else {
        pdf.addImage(domImg, 'JPEG', 5, 5, 200, 100)
    }

I do a page break and then shift it in height and add it to the page. The problem is that with such an offset, there are no indents and it turns out like this enter image description here

and I want to ensure that there are indents at the top and bottom.

Or is there an easier option to print the div?

I need to implement the window.print() functionality, but so that it immediately saves the pdf and does not open the window

0

There are 0 best solutions below