使用jsPdf Autotable时出错

时间:2016-07-22 10:18:03

标签: javascript html jspdf jspdf-autotable

我试图将数据打印到PDF中,所以我使用了jsPdf,那时数据没有正确对齐到我的PDF表格中。所以我在许多网站上搜索过他们让我使用jsPdf Auto-table。这里出现了问题,在注入jsPdf Auto-table之前,每个东西都工作正常(没有对齐)但是在我插入之后



 <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.0.16/jspdf.plugin.autotable.js"></script>
&#13;
&#13;
&#13;

这导致我的 index.html 收到错误,

  

未捕获的ReferenceError:未定义jsPDF(jspdf.plugin.autotable.js:10)

2 个答案:

答案 0 :(得分:3)

您需要在jspdf-autotable插件之前包含jspdf库 。有关详细信息,请参阅docs。您可能还想要最新版本。

&#13;
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.61/jspdf.debug.js"></script>

<!-- EDIT: For now, add this line between the libraries -->
<!-- The reason being that jspdf includes a version of requirejs which -->
<!-- jspdf-autotable currently is not expecting. You can also use version < 2.0.21 -->
<script>if (window.define) delete window.define.amd;</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.0.28/jspdf.plugin.autotable.js"></script>

<script>
var columns = ["ID", "Name", "Country"];
var rows = [
   [1, "Shaw", "Tanzania"],
   [2, "Nelson", "Kazakhstan"],
   [3, "Garcia", "Madagascar"]
];
var doc = new jsPDF('p', 'pt');
doc.autoTable(columns, rows);
doc.save('table.pdf');
</script>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

这与直接相关,但是对于那些试图在角度cli创建的Angular 4项目上使用它并且得到doc.autoTable不是函数的人。 在脚本数组中的.angular-cli.json文件中包含jspdf和jspdf.plugin.autotable

import * as jsPDF from 'jspdf';
import * as jpt from 'jspdf-autotable';

然后在你想要使用的组件中首先导入jspdf和jspdf-autotable

let doc = new jsPDF('p', 'pt');    jpt; 
doc.autoTable(this.columns, this.data);
doc.text(20, 20, 'Hello world!');
doc.text(20, 40, 'This is client-side Javascript, pumping out a PDF.');

// Save the PDF
doc.save('Test.pdf');

然后使用如下。 您必须在进行doc.autotable调用之前声明jpt变量

LocalPrintServer server = new LocalPrintServer(); //To find the nearby printers
PrintQueueCollection queuecollect = server.GetPrintQueues(); //collects all the printer queues on the machine
string queuename = null; //this gets set to the printer we want
foreach (PrintQueue pq in queuecollect)
{
    if (pq.FullName == _printername) //printername is from the config
    {
        queuename = pq.FullName; //pq goes down the list of queues found until it matches
    }
}
_Log.Debug("queuename: " + queuename); //Just so I can see it
PrintQueue queue = new PrintQueue(server, queuename); //this creates a queue for the specified printer on the local machine
byte[] bytes = System.IO.File.ReadAllBytes(dirpath); // The path to the PDF file
try
{
    PrintSystemJobInfo myprintjob = queue.AddJob(); //adds a job to the queue (This is the line 908 mentioned in the error)
    Stream jobstream = myprintjob.JobStream; //creates a stream to write data to the job
    jobstream.Write(bytes, 0, bytes.Length);
    jobstream.Close(); //close it, or else.
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}