代码之家  ›  专栏  ›  技术社区  ›  Alessandro Dentella

jsPDF:html2canvas在使用new.html()方法时未加载

  •  1
  • Alessandro Dentella  · 技术社区  · 7 年前

    我想用 jsPDF.html 要将html页面转换为pdf,我使用以下代码:

    savePdf () {
      var pdf = new jsPDF({unit: 'mm', format: 'a4', orientation: 'portrait' })
      pdf.html(document.getElementById('printable-cv'), {
        callback: function (pdf) {
          pdf.save('cv-a4.pdf');
        }
      })
    }
    

    html2canvas not loaded 是我忘了什么吗?我有html2canvas

    html2canvas“^1.0.0-alpha.12”

    在我当前使用的同一页中 html2pdf

    savePdf0 () {
      let opt = {
        filename: 'cv.pdf',
        enableLinks: true,
        image: { type: 'jpeg', quality: 0.98 },
        html2canvas: {
          scale: 8,
          useCORS: true,
          width: 310,
          letterRendering: true,
        },
        jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' },
      }
      html2pdf().set(opt).from(document.getElementById('printable-cv')).save()
    }
    

    正确地找到html2canvas。

    什么是 html2canvas未加载 真的吗?我能做些什么来加载它?

    2 回复  |  直到 5 年前
        1
  •  20
  •   ax_pernot    7 年前

    window.html2canvas = html2canvas;
    

    在你打电话之前的某个地方 html() .

    也就是说,我也无法让它工作,所以我求助于 a wrapper 通过手动调用解决此问题 html2canvas() 然后将结果画布提供给jsPDF。

        2
  •  4
  •   Amit Merin    7 年前

    在前面的回答之后 ptidus ,这应该是有效的:

    saveAsPdf() {
      window.html2canvas = html2canvas;
      var doc = new jsPDF(
        'p', 'pt', 'a4'
      );
      doc.html(document.querySelector("body"), {
        callback: function(pdf) {
          pdf.save("cv-a4.pdf");
        }
      });
    }
    

    可能会有一些边缘问题,但我并没有对此进行太多探讨。 您还会注意到生成的PDF实际上是文本,而不是图像。

    code example

        3
  •  1
  •   Martiuh    6 年前

    对于所有正在使用 webpack 我所做的就是加上 html2canvas 到 ProvidePlugin . You can read about this here

    // webpack configuration
    plugins: [
        new webpack.ProvidePlugin({
            html2canvas: 'html2canvas'
        });
    ]
    
        4
  •  0
  •   Phani Kanth    6 年前

    1. //网页包配置 新的webpack.ProvidePlugin({ }); ]

    2. 窗口[“html2canvas”]=html2canvas;

    即使没有迈出第一步,它也会起作用。