1

Web ページのファイルを生成しようとしていpdfますが、ローカル ディスクに保存して後で電子メールで送信したいと考えています。

私はこのアプローチを試みましたが、ここでの問題は、このようなページでは機能しないことです。を生成できpdfますが、Web ページのコンテンツと一致しません。

pdf前に生成されdocument readyたものか、別のものである可能性があることは非常に明確です。私は正確な問題を理解することができません。Webページの出力を.xmlとして保存できるアプローチを探していますpdf

では、Web ページの生成pdfの方が適しているといいのnodeですphpが。の解決策phpが利用可能であれば、それは大きな助けになるか、ノードの実装でも問題ありません。

4

2 に答える 2

3

ドキュメントの準備が整う前にpdfが生成されることは非常に明確です

非常に正しいので、スクリプトが読み込まれて実行されるまで待つ必要があります。


ファントムノードモジュール を使用する回答にリンクしました。

モジュールはその後アップグレードされ、スクリプトをより読みやすくする async/await 関数をサポートするようになりました。

async/await バージョン(バージョン 4.x、ノード 8+ が必要)を使用するソリューションを提案する場合があります。

const phantom = require('phantom');

const timeout = ms => new Promise(resolve => setTimeout(resolve, ms));

(async function() {
  const instance = await phantom.create();
  const page = await instance.createPage();

  await page.property('viewportSize', { width: 1920, height: 1024 });

  const status = await page.open('http://www.chartjs.org/samples/latest/charts/pie.html');

  // If a page has no set background color, it will have gray bg in PhantomJS
  // so we'll set white background ourselves
  await page.evaluate(function(){
      document.querySelector('body').style.background = '#fff';
  });

  // Let's benchmark
  console.time('wait');

  // Wait until the script creates the canvas with the charts
  while (0 == await page.evaluate(function(){ return document.querySelectorAll("canvas").length }) )  {
      await timeout(250);
  }

  // Make sure animation of the chart has played
  await timeout(500);

  console.timeEnd('wait');

  await page.render('screen.pdf');

  await instance.exit();
})();

私の開発マシンでは、チャートの準備が整うまで 600 ミリ秒かかります。await timeout(3000)toまたは他の任意の秒数よりもはるかに優れています。

于 2018-05-09T08:10:36.313 に答える