Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

node.js - how to take a print of server side response file on click of button?

I have created a webpage with react and material ui. on webpage I have defined print icon. I have to print file on click of print icon. File is fetched from backend as a response. I dont know how to print a file? below is function code on print icon click

const printFiles = (email:any,data:any) => {
    var request = new Request('http://localhost:3030/node/express/companyfiles/download/' + email + '/', {
        method:'GET',
        headers: new Headers({ 'Content-Type': 'application/zip',}),
       })
fetch(request)
 .then(response => ( response.json() ))
    
 .then(data)
   console.log(data)
 }
question from:https://stackoverflow.com/questions/66059358/how-to-take-a-print-of-server-side-response-file-on-click-of-button

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

JavaScript doesn't have any methods of interacting with output devices, which means you cannot use plain JS to print. You can however use a library like Print.js at Printjs.

Install it like so

npm install print-js --save

Import it to your component like so

import printJS from 'print-js'

then pass it the file/data you need printed

printJS('docs/PrintJS.pdf')

In your case

    var request = new Request('http://localhost:3030/node/express/companyfiles/download/' + email + '/', {
        method:'GET',
        headers: new Headers({ 'Content-Type': 'application/zip',}),
       })
fetch(request)
 .then(response => ( response.json() ))
    
 .then(data)
   printJS(data)
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...