简体   繁体   中英

javascript to generate pdf from html string

I have an generated a html page as a string which is stored in a variable (let's call it var htmlPage) I want to convert it to a pdf which I can later download

var htmlPage = '<!DOCTYPE html>\n'+
'<html lang="en" dir="ltr">\n'+
  '<head>\n'+
    '<meta charset="utf-8">\n'+
    '<title></title>\n'+

    '<style>\n'+
    '.hyperlink{\n'+
      'color: blue;\n'+
      'cursor: pointer;\n'+
    '}\n'+
   '</style>\n'+
  '</head>\n'+
  '<body>\n'+
    '<h1>Hello world!</h1>\n'+
  '</body>\n'+
'</html>';

The way you want it is actually not possible. Alternate way is to write it in a div and hide the div, once the user clicks print, display the div and hide everything else before printing. Doing so the div you want to print will get printed

Try using jsPDF

Check these links.. It may help..

Documentation

HTML to PDF - post on micropyramid

Check this stackoverflow thread

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM