简体   繁体   English

javascript从html字符串生成pdf

[英]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我有一个生成的 html 页面作为字符串存储在一个变量中(我们称之为 var htmlPage)我想将它转换为 pdf,我以后可以下载

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. 另一种方法是将其写入div并隐藏div,一旦用户单击“打印”,则显示div并隐藏其他所有内容,然后再进行打印。 Doing so the div you want to print will get printed 这样,您要打印的div将被打印

Try using jsPDF 尝试使用jsPDF

Check these links.. It may help.. 检查这些链接。可能会有所帮助。

Documentation 文献资料

HTML to PDF - post on micropyramid HTML转换为PDF-在微金字塔上发布

Check this stackoverflow thread 检查此stackoverflow线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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