Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 试图修改js函数,使其输出与调用另一个js函数时相同_Javascript_Jquery_Angularjs_Jspdf_Html2pdf - Fatal编程技术网

Javascript 试图修改js函数,使其输出与调用另一个js函数时相同

Javascript 试图修改js函数,使其输出与调用另一个js函数时相同,javascript,jquery,angularjs,jspdf,html2pdf,Javascript,Jquery,Angularjs,Jspdf,Html2pdf,我正在将网页上的内容导出到PDF文件,为此我使用了jsPDF API,我可以让它工作,但现在我想使用html2PDF,因为它解决了使用jsPDF API时遇到的一些问题 我已经编写了函数$scope.exportUsingJSPDF,单击按钮exportUsingJSPDF时调用该函数。类似地,我想实现函数$scope.exportUsingHTML2PDF,该函数使用html2PDF API,但未能成功。有关如何修改$scope.exportUsingHTML2PDF的任何输入,以便它迭代di

我正在将网页上的内容导出到PDF文件,为此我使用了jsPDF API,我可以让它工作,但现在我想使用html2PDF,因为它解决了使用jsPDF API时遇到的一些问题

我已经编写了函数
$scope.exportUsingJSPDF
,单击按钮exportUsingJSPDF时调用该函数。类似地,我想实现函数
$scope.exportUsingHTML2PDF
,该函数使用html2PDF API,但未能成功。有关如何修改
$scope.exportUsingHTML2PDF
的任何输入,以便它迭代div并显示div内容,如使用$scope.exportUsingJSPDF调用时所示,方法是单击Export using JSPDF API

完整的在线示例:

js代码:

    //trying to implement the below function same as $scope.exportUsingJSPDF, so
// that when user click on Export using HTML2PDF button, it exports the content to the PDF and generaes the PDF.
         $scope.exportUsingHTML2PDF = function(){
          var pdf = new jsPDF('l', 'pt', 'a4');
            var pdfName = 'test.pdf';

         pdf.canvas.height = 72 * 11;
         pdf.canvas.width = 72 * 8.5;
         html2pdf(document.getElementByClassName("myDivClass"), pdf, function(pdf){
             pdf.save(pdfName);
         });
         }

        $scope.exportUsingJSPDF = function() {
              var pdf = new jsPDF('p','pt','a4');
                var pdfName = 'test.pdf';

                var options = {   pagesplit: true};

                var $divs = $('.myDivClass')                //jQuery object of all the myDivClass divs
                var numRecursionsNeeded = $divs.length -1;     //the number of times we need to call addHtml (once per div)
                var currentRecursion=0;

                //Found a trick for using addHtml more than once per pdf. Call addHtml in the callback function of addHtml recursively.
                function recursiveAddHtmlAndSave(currentRecursion, totalRecursions){
                    //Once we have done all the divs save the pdf
                    if(currentRecursion==totalRecursions){
                        pdf.save(pdfName);
                    }else{
                        currentRecursion++;
                        pdf.addPage();
                        //$('.myDivClass')[currentRecursion] selects one of the divs out of the jquery collection as a html element
                        //addHtml requires an html element. Not a string like fromHtml.
                        pdf.fromHTML($('.myDivClass')[currentRecursion], 15, 20, options, function(){
                            console.log(currentRecursion);
                            recursiveAddHtmlAndSave(currentRecursion, totalRecursions)
                        });
                    }
                }

                pdf.fromHTML($('.myDivClass')[currentRecursion], 15, 20, options, function(){
                    recursiveAddHtmlAndSave(currentRecursion, numRecursionsNeeded);
                });
            }

PS:我试图修改$scope.exportUsingHTML2PDF,以便它提供与单击“使用JSPDF导出”按钮时生成的输出相同的输出,该按钮调用函数$scope.exportUsingJSPDF。

问题在于使用
exportUsingHTML2PDF的函数,错误是您需要将html传递给
html2PDF
的函数。根据需要管理页面css

编辑:您有错误的库。请检查plunker中的html2pdf.js库

工作插销:


使用
JSPDF
HTML2PDF
,您必须习惯两种根本不同的编码样式:

  • JSPDF:命令式(javascript语句)
  • HTML2PDF:声明性(嵌入HTML中的指令)
因此,对于分页符:

  • JSPDF
    pdf.addPage()
  • HTML2PDF
这应该是可行的,但是
HTML2PDF
是有缺陷的,并且在包含
时会出现“提供的数据不是JPEG”错误(至少在Plunkr中对我来说是这样),尽管存在缺陷


我没有时间调试它。你需要做些调查。有人会在web上的某个地方发布解决方案。

请查找更新的演示和您的代码。它不显示图像,只显示第一个div数据。但是,当您单击Export Using JSPDF时,您会注意到生成的PDF包含所有div的数据,我重复了4次,每个div数据都显示在新页面中。很难获得HTML2PDF的相同结果,任何输入都是有用的。因为我调用了PDF.addPage();要为$scope.exportUsingJSPDF添加新页面,我需要对$scope.exportUsingHTML2PDF执行相同的操作,但无法使其正常工作。任何输入都会有帮助。我想在$scope.exportUsingHTML2PDF中实现与$scope.exportUsingJSPDF中相同的逻辑。@SunilLama@user7833845你错了,请查看我编辑的答案。这就是你想要的吗?请不要忘记向上投票,并勾选正确:p+1谢谢@Sunil Lama。我有几个id=“hideThis”的div,我不想在生成的PDF中显示,我已经包含了我用于导出的逻辑,使用JSPDF限制将给定div导出到PDF。另一个问题是在生成的PDF中,当新行从页面末尾开始时,我是否可以添加分页符或新页面,现在您可以注意到页面中的最后一行显示在两页中。如何从新页面开始每个div内容,如使用JSPDF导出所示。当然我会接受答案:)嗨@user7833845,我不知道库中封装了哪些函数。最好为此创建一个新线程:)
    $scope.exportUsingHTML2PDF = function() {
    var element = document.getElementById('element-to-print');
    html2pdf(element, {
        margin: 1,
        filename: 'myfile.pdf',
        image: {
            type: 'jpeg',
            quality: 0.98
        },
        html2canvas: {
            dpi: 192,
            letterRendering: true
        },
        jsPDF: {
            unit: 'in',
            format: 'letter',
            orientation: 'portrait'
        }
    });
}