web-dev-qa-db-ja.com

jsPDFライブラリを適切に使用する方法

Divの一部をPDFに変換したいのですが、jsPDFライブラリを試しましたが成功しませんでした。ライブラリを機能させるためにインポートする必要があるものを理解できないようです。私は例を見てきましたが、まだ理解できません。私は次を試しました:

<script type="text/javascript" src="js/jspdf.min.js"></script>

JQueryの後と:

$("#html2pdf").on('click', function(){
    var doc = new jsPDF();
    doc.fromHTML($('body').get(0), 15, 15, {
        'width': 170
    });
    console.log(doc);
});

テスト目的のためですが、私は受け取ります:

"Cannot read property '#smdadminbar' of undefined"

ここで、#smdadminbarは本文の最初のdivです。

次のようにhtmlからpdfを使用できます。

ステップ1:次のスクリプトをヘッダーに追加します

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>

またはローカルでダウンロード

ステップ2:jsPDFコードを実行するHTMLスクリプトを追加する

これをカスタマイズして識別子を渡すか、単に#contentを必要な識別子に変更します。

 <script>
    function demoFromHTML() {
        var pdf = new jsPDF('p', 'pt', 'letter');
        // source can be HTML-formatted string, or a reference
        // to an actual DOM element from which the text will be scraped.
        source = $('#content')[0];

        // we support special element handlers. Register them with jQuery-style 
        // ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
        // There is no support for any other type of selectors 
        // (class, of compound) at this time.
        specialElementHandlers = {
            // element with id of "bypass" - jQuery style selector
            '#bypassme': function (element, renderer) {
                // true = "handled elsewhere, bypass text extraction"
                return true
            }
        };
        margins = {
            top: 80,
            bottom: 60,
            left: 40,
            width: 522
        };
        // all coords and widths are in jsPDF instance's declared units
        // 'inches' in this case
        pdf.fromHTML(
            source, // HTML string or DOM elem ref.
            margins.left, // x coord
            margins.top, { // y coord
                'width': margins.width, // max width of content on PDF
                'elementHandlers': specialElementHandlers
            },

            function (dispose) {
                // dispose: object with X, Y of the last line add to the PDF 
                //          this allow the insertion of new lines after html
                pdf.save('Test.pdf');
            }, margins
        );
    }
</script>

ステップ3:ボディコンテンツを追加する

<a href="javascript:demoFromHTML()" class="button">Run Code</a>
<div id="content">
    <h1>  
        We support special element handlers. Register them with jQuery-style.
    </h1>
</div>

元のチュートリアルを参照

作業フィドルを参照

113
Well Wisher

このリンクのみが必要です jspdf.min.js

それにはすべてが含まれています。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
16
user890332

これは最終的に私のために何をしたかです(そして気質を引き起こします):

function onClick() {
  var pdf = new jsPDF('p', 'pt', 'letter');
  pdf.canvas.height = 72 * 11;
  pdf.canvas.width = 72 * 8.5;

  pdf.fromHTML(document.body);

  pdf.save('test.pdf');
};

var element = document.getElementById("clickbind");
element.addEventListener("click", onClick);
<h1>Dsdas</h1>

<a id="clickbind" href="#">Click</a>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.3/jspdf.min.js"></script>

そして、KnockoutJSの傾向がある人のために、少し拘束力があります:

ko.bindingHandlers.generatePDF = {
    init: function(element) {

        function onClick() {
            var pdf = new jsPDF('p', 'pt', 'letter');
            pdf.canvas.height = 72 * 11;
            pdf.canvas.width = 72 * 8.5;

            pdf.fromHTML(document.body);

            pdf.save('test.pdf');                    
        };

        element.addEventListener("click", onClick);
    }
};
6
pim

Jspdf.plugin.from_html.jsライブラリも使用すべきではありませんか?メインライブラリ(jspdf.js)に加えて、「特別な操作」に他のライブラリを使用する必要があります(画像を使用するためのjspdf.plugin.addimage.jsなど)。 https://github.com/MrRio/jsPDF を確認してください。

3
Pedro Almeida

最初に、ハンドラーを作成する必要があります。

var specialElementHandlers = {
    '#editor': function(element, renderer){
        return true;
    }
};

次に、クリックイベントで次のコードを記述します。

doc.fromHTML($('body').get(0), 15, 15, {
    'width': 170, 
    'elementHandlers': specialElementHandlers
        });

var pdfOutput = doc.output();
            console.log(">>>"+pdfOutput );

既にdoc変数を宣言していると仮定します。そして、File-Pluginを使用してこのPDFファイルを保存しました。

3
Akshay Tyagi

vuejsではどうですか?

function onClick() {
  var pdf = new jsPDF('p', 'pt', 'letter');
  pdf.canvas.height = 72 * 11;
  pdf.canvas.width = 72 * 8.5;

  pdf.fromHTML(document.body);

  pdf.save('test.pdf');
};

var element = document.getElementById("clickbind");
element.addEventListener("click", onClick);
<h1>Dsdas</h1>

<a id="clickbind" href="#">Click</a>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.3/jspdf.min.js"></script>
1
Antwnina