I am using https://www.npmjs.com/package/react-qr-code to generate QR code on my website.
I want to change the size of this qr code when I print this qr code in PDF file. can I change the size of qr code in pdf file without changing the size on react page? I have tried changing the size using state but issue is that updating the state also updates the size on DOM container.
I am using https://github.com/gregnb/react-to-print to print the qr code.
import QRCode from 'react-qr-code';
import ReactToPrint from 'react-to-print';
const getPageMargins = () => {
return `@page { margin: '1000px' '10' '10 '10' !important; }`;
};
const onBeforeGetContent = () => {
setSize(200);
setIsLoading(true);
return Promise.resolve();
};
const onBeforePrint = () => {
setSize(100);
return Promise.resolve();
};
const onAfterPrint = () => {
setSize(100);
setIsLoading(true);
return Promise.resolve();
};
// this is what I am rendering
<div ref={QRRef}>
<QRCode
value={`{ "_id": "${unit.id}" }`}
size={size}
/>
<p className="hide-unit-id">{unit.id}</p>
</div>
<ReactToPrint
pageStyle={getPageMargins}
onBeforePrint={onBeforePrint}
onBeforeGetContent={onBeforeGetContent}
onAfterPrint={onAfterPrint}
trigger={() => (
<PrinterFilled />
)}
content={() => QRRef.current}>
</ReactToPrint>
Any help is much appreciated. Thanks in advance.
Better cleaner solution for this would be to use Print CSS. Adjust the size of the targeted element by setting CSS
width
andheight
properties, in following example I have set thewidth
andheight
of the QR code'ssvg
element to50%
and passing
pageStyle
property touseReactToPrint
hook