0

I am trying to Save ReactJS output(includes visual reports etc..) as PDF file.

I used html2canvas to take screenshot and then used jspdf for converting that to PDF. But the problem is when screen size is reduced(or in mobile view) even screenshots are of small size as it uses DOM styling. So PDF is not properly generated as it should be seen in Full View.

1 Answer 1

3

You can use dom-to-png plugin by npm to create your image first and then just put that image in your PDF File.

like this

export function printDetails(ref, fileName: string) {
    const pdf = new jsPDF('p', 'mm', 'a4');
    let height = pdf.internal.pageSize.height;
    let pageHeightInPixels = ref.clientHeight;
    let pageHeightInMM = pageHeightInPixels / 3.78;
    let pages = pageHeightInMM / height;
    const roundOff = Number(pages.toString().split('.')[1].substring(0, 1));
    const pageNo = (roundOff > 0 ? pages + 1 : pages);
    let pageCount = pages < 1 ? 1 : Math.trunc(pageNo);
    let imageHeight = height;
    domtoimage.toPng(ref, {
        height: ref.clientHeight,
        width: 665,
        style: {
            transform: 'unset',
            left: '0%',
            margin: 'unset',
            backgroundColor: 'white',
            maxHeight: '100%'
        },
    })
        .then(function (dataURL) {
            hidePrintPreviewModal();
            for (let i = 1; i <= pageCount; i++) {
                let pdfStartingHeight = height * (i - 1);
                pdf.addImage(dataURL, 'JPEG', 30, -pdfStartingHeight, 160, ref.clientHeight);
                if (i < pageCount) {
                    pdf.addPage();
                }
            }
            pdf.save(`${fileName}.pdf`);
        }).catch(function (error) {
            console.error('oops, something went wrong!', error);
        });
}

Using id

<html>
<body>
<div id="print">
{Whatever you want to print}
</div>
</body>
</html>

JS:

const ref = document.getElementById('print');
printDetails(ref, 'test')

Using ref

class Demo extends React.Component {

    refValue;

    printData = () => {
        const node = React.findDOMNode(this.refValue)
        printDetails(node, 'test');
    }

    render() {
        return (
            <div ref={(value) => { this.refValue = value }}>
                {Whatever you want to print}
    <button onClick={this.printData}>PRint</button>
            </div>

        )
    }
}

This function is using the dom to png and jspdf both to print the file. It is also calculating the no of pages so that nothing is missed.

You need to pass the node of your html element which you can get by ref or document.getElementById and that function will calculate the image with the height and width applied.

reference ->

dom-to-image

jspdf

Sign up to request clarification or add additional context in comments.

7 Comments

Giving one error => TypeError: node.cloneNode is not a function
N one more thing. what should I pass for "ref"??
ref the ID of your DOM node that you have to pass react provides ref i.e. reference to the DOM Node.
The dom-to-image assumes that the .toPng 's first argument if a DOM node and it clones it recursively with all the styles applied in it. So if the argument is not a node than the function cloneNode can not be called on that I think that's why you are getting this error.
I have added some extra code in my answer so that you can understand easily how that function is working.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.