Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用PDFKIT使用nodejs生成PDF_Javascript_Node.js_Pdf_Pdfkit - Fatal编程技术网

Javascript 使用PDFKIT使用nodejs生成PDF

Javascript 使用PDFKIT使用nodejs生成PDF,javascript,node.js,pdf,pdfkit,Javascript,Node.js,Pdf,Pdfkit,我编写了一个node.js服务器,使用pdfkit生成和返回PDF,而不保存到文件: var http = require('http'); var PDFDocument = require('pdfkit'); var server = http.createServer(); server.on('request', function(request, response) { console.log('request - ' + new Date()); buildRe

我编写了一个node.js服务器,使用pdfkit生成和返回PDF,而不保存到文件:

var http = require('http');
var PDFDocument = require('pdfkit');

var server = http.createServer();

server.on('request', function(request, response) {
    console.log('request - ' + new Date());
    buildReport(request, response);
});

server.listen(3000);

console.log('Servidor iniciado em localhost:3000. Ctrl+C para encerrar…');

function buildReport(request, response) {
    var PDFDoc = new PDFDocument();

    response.writeHead(200, {
        'Content-Type': 'application/pdf',
        'Access-Control-Allow-Origin': '*'
    });

    PDFDoc.pipe(response);

    PDFDoc.font('fonts/DroidSans.ttf')
          .fontSize(25)
          .text('Some text with an embedded font!', 100, 100);

    // Finalize PDF file
    PDFDoc.end();
}
如果我转到浏览器并键入
http:\\localhost:3000
,它可以正常工作。PDF将显示在浏览器窗口中

但问题是我必须将这个请求的结果放在一个iframe中。下一行是:

$('#iframe').attr('src', 'http://localhost:3000');
但是,由于我必须将参数传递给服务器,所以我使用的是ajax,然后它就不起作用了。在客户端,我有:

$.ajax({
    url: 'http://localhost:3000',
    data: {
        reportData: reportData,
        reportMetadata: reportMetadata,
        reportLogo: reportLogo
    }
}).done(function(data) {
    $('#iframe').attr('src', data);
});
但这是行不通的。我尝试将“完成”函数更改为:

.done(function(data) {
    var PDFOutput = 'data:application/pdf;charset=UTF-8,'+data;
    $('#iframe').attr('src', PDFOutput);
});
但它也不起作用

返回到
done
函数中的数据变量的值是一个以以下开头的字符串:

"%PDF-1.3 %���� 5 0 obj << /Type /Page /Parent 1 0 R /MediaBox [0 0 612 792] /Contents 3 0 R /Resources 4 0 R >> endobj 4 0 obj << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F2 6 0 R >> >> endobj 7 0 obj << /Producer (PDFKit) /Creator (PDFKit) /CreationDate (D:20151010210841Z) >> endobj 9 0 obj << /Type /FontDesc (...)

%PDF-1.3%���� 你不能做那样的事

done
函数中,
data
包含PDF文件的正文

iframe应该从url加载内容,因此使用JQuery只需更改
src
属性,如下所示:

$('#iframe').attr('src', 'http://localhost:3000?reportData='+reportData+'& reportMetadata='+reportMetadata+'&reportLogo='+reportLogo);

因为您不能
数据发布到iframe(不是以干净的方式)您必须使用
GET
参数。可能您要传递的数据太重,无法通过
GET
传递,因此您必须找到另一种方法…

谢谢Raphael…此解决方案有效,但我想知道,实际上,我是否无法将PDF的正文转换为对象URL,如
data:application/PDF;base64,AAAAAAAAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa。