Electron アプリケーションで pdf ドキュメントを作成しようとしています。ユーザーが JSON ファイルを選択すると、それを解析して新しい PDF ドキュメントを作成します。ドキュメントを作成しようとするとエラーが発生します。
Uncaught Error: Type of text must be string or Array. "undefined" is not recognized.
at Object.H.text (/Users/antarrbyrd/dev/pathway_exporter/bower_components/jspdf/dist/jspdf.min.js:1)
at HTMLInputElement.<anonymous> (index.html:48)
at HTMLInputElement.dispatch (/Users/antarrbyrd/dev/pathway_exporter/lib/jquery.min.js:3)
at HTMLInputElement.q.handle (/Users/antarrbyrd/dev/pathway_exporter/lib/jquery.min.js:3)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="./bower_components/photon/dist/css/photon.min.css" rel="stylesheet" type="text/css" />
<title>Seremedi - Pathway Exporter</title>
</head>
<body>
<div class="window">
<header class="toolbar toolbar-header">
<div class="btn-group">
<button class="btn btn-large btn-default" id="open-button">
<span class="icon icon-folder"></span>
</button>
<button class="active btn btn-large btn-default">
<span class="icon icon-download"></span>
</button>
<button class="active btn btn-large btn-default">
<span class="icon icon-print"></span>
</button>
</div>
<input type="file" id="file-input" style="display: none;"></input>
</header>
<div class="window-content">
<webview id="webview" autosize style="display:inline-flex; width:100%; height:100%"></webview>
</div>
<footer class="toolbar toolbar-footer">
<h1 class="title">© Seremedi - 2017</h1>
</footer>
</div>
</body>
<script>
$ = require('jquery')
jsPDF = require('jspdf')
PDFDocument = require('pdfkit')
$('#open-button').on('click',function(evt){
evt.preventDefault();
$('#file-input').click();
});
$('#file-input').on('change',function(evt){
var location = $('#file-input')[0].files[0].path;
source = "";
$('#webview').prop('src', location);
$.getJSON(location, function(json){
source = json;
console.log(source);
});
var doc = new jsPDF();
doc.text(source.ReferenceId, 10, 10);
doc.save('file.pdf');
});
</script>
</html>
Javascript は非同期です。jsPDF 行は、$.getJSON()
終了する前に実行されます。その時点でsource
は、ただの空文字列です。したがってsource.ReferenceId
、未定義です。
$.getJSON(location, function(json){
source = json;
var doc = new jsPDF();
doc.text(source.ReferenceId, 10, 10);
doc.save('file.pdf');
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加