问题描述
想法是打印我在我创建的 pdfviewer 组件中显示的 pdf 文档。我从服务器获取 Uint8Array 并在组件内部呈现。一切正常,到目前为止一切顺利。
问题始于在我的 pdf 查看器中实现打印功能。我正在将带有 react-pdf 库的 pdf 渲染为 SVG。
const PdfViewer = (props) => {
// removed code that is not important for this issue
const elmRef= useRef()
// removed code that is not important for this issue
const handlePrint = useReactToPrint({
content: () => elmRef.current
});
//const handlePrint = useReactToPrint({
return (
<Dialog className="dialogBox" title={props.dataItem[props.field]} onClose={props.modalAction} height={'95vh'} width={'90vw'} onClose={props.onClose}>
<PdfDocumentControls
handlePrint={handlePrint}
/>
<BasePdfViewer className='pdfViewer' pageNumber={pageNumber} file={file} onDocumentLoadSuccess={onDocumentLoadSuccess} zoom={zoom} refElm={elmRef} />
{props.children}
</Dialog>
);
};
当“handlePrint”触发时,我收到对话框,但似乎无法渲染 svg。
问题:有没有办法将这个 SVG 正确渲染为打印框内的文本?
解决方法
解决方案:像往常一样,经过几天的挣扎,刚刚在 SO 上发布问题后,大脑开始运作,我想出了解决方案。
将 pdf 渲染为画布而不是 SVG 可以解决问题,并且 pdf 会按预期打印出来。
泰。干杯