简体   繁体   English

如何在javascript中使用html2pdf从div HTML制作PDF,但对于移动和PC视图相同

[英]How can I make PDF from div HTML using html2pdf in javascript, but the same for mobile and pc views

I need export a div tag as PDF, and I am using html2pdf, but when I export from mobile devices the view is different that when export pc (in mobile devices is smaller).我需要将 div 标签导出为 PDF,并且我使用的是 html2pdf,但是当我从移动设备导出时,视图与导出 pc 时的视图不同(在移动设备中较小)。

My Code HTML y JS:我的代码 HTML y JS:

      <div id="contentToExport" >
        <div class="row">
            <div class="col-lg-8 col-xs-12 mt-10" >
                          <canvas id="chartPie" width="1300" height="600"></canvas>
            </div>
        </div>
        <div class="row">
          <div class="col-lg-6 col-lg-offset col-xs-offset-1 mt-10" style="border: none">
            <div class="panel panel-default card-view">
                <div class="panel-heading">
                <div class="pull-left">
                  <h6 class="panel-title txt-dark">Elements list</h6>
                </div>
                <div class="clearfix"></div>
              </div>
                <div class="panel-wrapper collapse in">
                  <div class="panel-body row">
                    <div class="">
                        ...List elements   
                      <hr class="light-grey-hr mt-0 mb-15"/>
                    </div>
                  </div>
                </div>
            </div>
          </div>
        </div>
      </div>

Mi JS code:米JS代码:

  var element = document.getElementById('contentToExport');
  var opt={
    margin:     [2,1],
  filename:     'Report.pdf',
  image:        { type: 'png', quality: 1.0 },
  html2canvas:  { scale: 2 },
  jsPDF:        { unit: 'in', format: 'letter', orientation: 'portrait' }
   }
  html2pdf(element).set(opt).save();

I am using a node lib for converting html to pdf html-pdf .我正在使用节点库将 html 转换为 pdf html-pdf However, my env.但是,我的环境。 is node and express.是节点和表达。

 var pdf = require('html-pdf);

router.route("/pdf/:templateName")
    .post(jsonParser, function(req, res) { //create pdf + upload to AWS + retrun the url.
        var doc = req.body;
        var html = fs.readFileSync('./views/' + req.params.template, 'utf8');
        var buf = ejs.render(html, doc);

        var options = {
            format: "Letter", // allowed units: A3, A4, A5, Legal, Letter, Tabloid 
            orientation: "portrait", // portrait or landscape  
            header: {
                height: '30mm',
                contents: ''
            },
        footer: {
            height: '16mm',
            contents: '<div style="border-top:1px solid grey;margin: 0 auto;width:558px;"></div><div style="width:720px;margin:0 auto;padding-top:2mm;">' + addressBar + '</div>'
        }
    };
    pdf.create(buf, options).toFile('./tempateName.pdf', function(err, res) {
        if (err) return console.log(err);
        console.log(res); // { filename: '/app/businesscard.pdf' }
      });
});

let me know if it works or not.让我知道它是否有效。

I had a similar problem.我有一个类似的问题。 I was able to solve it by passing properties for我能够通过传递属性来解决它

html2canvas: {scale: 2, scrollX: 0, scrollY: 0, width: 596}

to remove the scroll, when the screen is resized by media queries or width, to prevent this scaling.当屏幕被媒体查询或宽度调整大小时,移除滚动,以防止这种缩放。
In my case, the pdf A4 design needed to be the same for both desktop and mobile.就我而言,桌面和移动设备的 pdf A4 设计需要相同。

library: html2pdfjs库:html2pdfjs

version: 0.9.2版本:0.9.2

const exportToPDF = (fileName) => {
    let opt = {
      margin: 0.5,
      filename: `${fileName}.pdf`,
      image: {type: 'jpeg', quality: 1},
      html2canvas: {scale: 2, scrollX: 0, scrollY: 0, width: 596},
      jsPDF: {unit: 'in', format: 'A4', orientation: 'portrait'}
    };

    html2pdf().set(opt).from(pdfContainerRef.current).save();
}

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

相关问题 如何使 pdf 的高度和宽度与 html 相同,同时使用 html? - How to make the pdf height and width same as html while converting html to pdf using html2pdf? html2pdf:如何隐藏<div>从PDF? - html2pdf: How To Hide <div> From PDF? 如何使用 html2pdf 从 dataURL 下载 PDF 格式的图像? - How do I download an image in PDF format from dataURL using html2pdf? 如何在vue3js中使用html2pdf从html中制作pdf文件? - How to make a pdf file out of html using html2pdf in vue3js? 如何根据class大小制作pdf文件(html2pdf) - how to make pdf file according to class size (html2pdf) 如何使用jspdf,html2pdf,pdfmaker,html2canvas以外的JavaScript库生成pdf? - How to generate pdf using javascript libraries other than jspdf, html2pdf, pdfmaker, html2canvas? 如何使用 html2pdf 生成 PDF 文件(就像文件一样) - How to generate a PDF File (just as a File) using html2pdf 如何在本地保存使用带节点的 html2pdf 生成的 pdf? - How can I save locally the pdf that I have generated with html2pdf with node? 我可以从 html2pdf 在当前或新选项卡中打开 pdf 而不是下载它(打开弹出窗口) - can I open the pdf in current or new tab from html2pdf instead of downloading it (open pop up ) 如何防止 HTML2PDF 自动下载 pdf? - How do I prevent HTML2PDF from auto-downloading the pdf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM