使用PDFKIT使用nodejs生成PDF

时间:2021-08-17 09:48:21

I wrote a node.js server to generate and PDF using pdfkit and return it without saving to a file:

我编写了一个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();
}

If I go to the browser and type http:\\localhost:3000 it works fine. The PDF appears on the browser window.

如果我去浏览器并键入http:\\ localhost:3000它可以正常工作。 PDF显示在浏览器窗口中。

But the problem is that I have to put the result of this request inside an iframe. This next line works:

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

$('#iframe').attr('src', 'http://localhost:3000');

But since I have to pass parameters to the server I'm using ajax and then it does not work. On the client side I have:

但是因为我必须将参数传递给服务器,所以我使用的是ajax然后它不起作用。在客户端我有:

$.ajax({
    url: 'http://localhost:3000',
    data: {
        reportData: reportData,
        reportMetadata: reportMetadata,
        reportLogo: reportLogo
    }
}).done(function(data) {
    $('#iframe').attr('src', data);
});

but this does not work. I tried alter the done function to:

但这不起作用。我尝试将done函数改为:

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

but it does not work either...

但它也不起作用......

The value returned to the data variable inside done function is a string that starts with:

返回到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 (...)

What am I missing?

我错过了什么?

Thanks in advance!

提前致谢!

1 个解决方案

#1


0  

You cannot do something like that.

你不能做那样的事情。

In your done function, data contains the body of your PDF file.

在完成的函数中,数据包含PDF文件的正文。

An iframe is supposed to load content from an url, so with your JQuery, just change the src attribute like this :

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

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

As you can't POST data to an iframe (not by a clean way) you will have to use GET parameters. Maybe the datas your are passing are to heavy to be passed by GET so you will have to find an another way...

由于您无法将数据发布到iframe(而不是通过干净的方式),因此您必须使用GET参数。也许你传递的数据很重,要通过GET传递,所以你必须找到另一种方式......

#1


0  

You cannot do something like that.

你不能做那样的事情。

In your done function, data contains the body of your PDF file.

在完成的函数中,数据包含PDF文件的正文。

An iframe is supposed to load content from an url, so with your JQuery, just change the src attribute like this :

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

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

As you can't POST data to an iframe (not by a clean way) you will have to use GET parameters. Maybe the datas your are passing are to heavy to be passed by GET so you will have to find an another way...

由于您无法将数据发布到iframe(而不是通过干净的方式),因此您必须使用GET参数。也许你传递的数据很重,要通过GET传递,所以你必须找到另一种方式......