2017-03-05 5 views
3

jqueryを使用してdocument.readyにページをロードし、cssを使用してページをフォーマットし、node.js httpを使用してページを取得します。サーバ、html-pdf、htmlをpdfファイルに変換します。この時点から、ファイルを電子メールの添付ファイルとして送信しています。究極の目標は請求書を作成することです。問題は、cssとjsが外部ファイルにあるときに処理されていないように見えることです。私は外部ファイルを保持するが、jqueryをdocument.readyとページの適切な書式を設定するCSSに読み込む必要があります。また、ミキシングにcheerioを追加して、処理が正しく行われたかどうかを確認し、それが役に立たないかどうかを確認しました。私は、電子メールをチェックするとhtml-pdf:cssとjsファイルは処理されていません

email sending code: 
    var transporter = nodemailer.createTransport({ 
     service: 'gmail', 
     host: this.hostname, 
     auth: { 
      user: this.smtpusername, 
      pass: this.smtppassword 
     } 
    }); 

    var mailOptions = { 
     from: fromaddress, // sender address 
     to: toaddresslist, // list of receivers 
     subject: subject, // Subject line 
     html: text 
    }; 

    if (attachments){ 
     mailOptions.attachments = [ 
      { // binary buffer as an attachment 
       filename: 'invoice.pdf', 
       content: attachments, 
       contentType: 'application/pdf', 
       encoding: 'base64' 
      } 
     ]; 
    } 

html conversion code: 
       let body = []; 
       var options = { 
        host: 'localhost', 
        path: '/invoice/' + invoiceid, 
        port: '3000' 
       }; 

       http.request(options, function(response){ 
        response.on('data', function (chunk) { 
         body.push(chunk); 
        }); 

        response.on('end', function() { 
        let buffer = ''; 
        buffer = Buffer.concat(body).toString(); 
        let $ = cheerio.load(buffer); 
        let newdocument = $.html(); 
        console.log(newdocument); 

        pdf.create(newdocument, { 
         directory: "tmp", 
         format: "letter", 
         orientation: "portrait", 
         zoomFactor: ".5", // default is 1 
         type: "pdf", 
         quality: "75" 
        }).toBuffer(function(err, newbuffer){ 
         if (err){ 
          reject(err); 
         } 

         if (Buffer.isBuffer(newbuffer)){ 
          resolve(newbuffer); 
         } else { 
          reject(new Error('The pdf file could not be generated at this time. Please try again later.')); 
         } 
        }); 
        }); 
       }) 
       .end(); 

、私はPDFファイルを取得していますが、私は、API呼び出しから期待するデータが移入し、フォーマットが道オフになって表示されませんされています。コードは以下に概説されます。私はテスト目的のために文書の中にCSSコードを移動しようとしましたが、CSSはまだオフになっていましたが、このシナリオではCSSを見ることができました。

1)外部のcssファイルとjsファイルを認識して処理するには、どうすればhtml-pdfパッケージを入手できますか?

2)html-pdfがどのような種類のhtml書式を使用していますか?

+0

外部ファイルへのリンクは、html-pdfを実行している場所からアクセスできますか?それらに明示的なパスを使用していますか? – Martina

答えて

1

あなたのCSSファイルの絶対リンクを使用しようとしたことがありますか? htmlのheadセクションで?

<link rel='stylesheet' type='text/css' href='http://www.url.com/css/my.css' /> 

使用http://localhost:3000は、ローカルホストを使用している場合もあります。

nodejsサーバー構成にパスが正しく設定されていることを確認してください。 (そうしないと、404エラーが発生します)。

編集:このスレッドはリポジトリにあります。 https://github.com/marcbachmann/node-html-pdf/issues/13

これが役に立ちます。

1

基本オプションを使用していますか?あなたのcssとjsはサーバー上で利用できますか?また、あなたのjavascriptは長時間実行されているスクリプト、多分AJAXや何か時間がかかる? JSが終了するのを、HTML、PDFファイルがどのように良いわからないが、それを待つしないことがあり、いくつかのライブラリは、そのためのオプションがあります。

// Rendering options 
     "base": "file:///home/www/your-asset-path", // Base path that's used to load files (images, css, js) when they aren't referenced using a host 

https://www.npmjs.com/package/html-pdf

関連する問題