最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何从创建的PDF文件发送base64字符串到客户端?

IT培训 admin 4浏览 0评论

如何从创建的PDF文件发送base64字符串到客户端?

我有一个node.js / express.js应用程序,其中一个路由使用pdfkit创建一个pdf文件。这有效,pdf文件作为文件写在我的文件系统中。但是,我需要的流程是没有文件写入文件系统,而是可以发送到客户端的base64字符串。

但是,以下方法为我创建了一个有效的pdf文件,但base64字符串始终为空。如何才能做到这一点?

const PDFDocument = require('pdfkit');
// Create a document
const doc = new PDFDocument;

// Add an image, constrain it to a given size, and center vertically and horizontally

doc.pipe( fs.createWriteStream('out.pdf') );

// Add another page
 doc.addPage()
 .fontSize(25)
 .text('Here is some vector graphics...', 100, 100);

 // Draw a triangle
 doc.save()
 .moveTo(100, 150)
 .lineTo(100, 250)
 .lineTo(200, 250)
 .fill("#FF3300");

 // Apply some transforms and render an SVG path with the 'even-odd' fill rule
  doc.scale(0.6)
  .translate(470, -380)
  .path('M 250,75 L 323,301 131,161 369,161 177,301 z')
  .fill('red', 'even-odd')
  .restore();

// Add some text with annotations
 doc.addPage()
  .fillColor("blue")
  .text('Here is a link!', 100, 100)
  .underline(100, 100, 160, 27, {color: "#0000FF"})
  .link(100, 100, 160, 27, '/');

  // Finalize PDF file
  doc.end();

// Create base64
 function base64Encode(file) {
   var body = fs.readFileSync(file);
  return body.toString('base64');
}


 var base64String = base64Encode('out.pdf');
 console.log(base64String);

// API result object to be sent to client
res.status(200).json({
 status: "success",
 data: base64String, 
 message: "Congrats! "
 });
回答如下:

经过一番游戏,我找到了一个有效的解决方案。

// you pdf settings here.
let options = {
  margins: 50
}

// create doc with settings
const doc = new PDFDocument(options);
// create stream and pipe doc
let out = fs.createWriteStream('output.pdf')
doc.pipe(out);

// create the pdf content
doc.addPage()
   .fontSize(25)
   .text('Here is some vector graphics...', 100, 100);

 // finalize document
 doc.end();

 // Stream on finish event handling after document construction ended
 out.on('finish', function() {

  // read pdf file as base64
  fs.readFile('output.pdf', 'base64', function(err, data) {
  if (err) throw err;
  console.log(data);

  // Send base64 pdf to client
    res.status(200).json({
      status: "success",
      data: data,
      message: "Congrats! "
    });
});
});

我使用这里的答案(@CHACO)从其base64-string构造pdf文件客户端:

how to display base64 encoded pdf?

如何从创建的PDF文件发送base64字符串到客户端?

我有一个node.js / express.js应用程序,其中一个路由使用pdfkit创建一个pdf文件。这有效,pdf文件作为文件写在我的文件系统中。但是,我需要的流程是没有文件写入文件系统,而是可以发送到客户端的base64字符串。

但是,以下方法为我创建了一个有效的pdf文件,但base64字符串始终为空。如何才能做到这一点?

const PDFDocument = require('pdfkit');
// Create a document
const doc = new PDFDocument;

// Add an image, constrain it to a given size, and center vertically and horizontally

doc.pipe( fs.createWriteStream('out.pdf') );

// Add another page
 doc.addPage()
 .fontSize(25)
 .text('Here is some vector graphics...', 100, 100);

 // Draw a triangle
 doc.save()
 .moveTo(100, 150)
 .lineTo(100, 250)
 .lineTo(200, 250)
 .fill("#FF3300");

 // Apply some transforms and render an SVG path with the 'even-odd' fill rule
  doc.scale(0.6)
  .translate(470, -380)
  .path('M 250,75 L 323,301 131,161 369,161 177,301 z')
  .fill('red', 'even-odd')
  .restore();

// Add some text with annotations
 doc.addPage()
  .fillColor("blue")
  .text('Here is a link!', 100, 100)
  .underline(100, 100, 160, 27, {color: "#0000FF"})
  .link(100, 100, 160, 27, '/');

  // Finalize PDF file
  doc.end();

// Create base64
 function base64Encode(file) {
   var body = fs.readFileSync(file);
  return body.toString('base64');
}


 var base64String = base64Encode('out.pdf');
 console.log(base64String);

// API result object to be sent to client
res.status(200).json({
 status: "success",
 data: base64String, 
 message: "Congrats! "
 });
回答如下:

经过一番游戏,我找到了一个有效的解决方案。

// you pdf settings here.
let options = {
  margins: 50
}

// create doc with settings
const doc = new PDFDocument(options);
// create stream and pipe doc
let out = fs.createWriteStream('output.pdf')
doc.pipe(out);

// create the pdf content
doc.addPage()
   .fontSize(25)
   .text('Here is some vector graphics...', 100, 100);

 // finalize document
 doc.end();

 // Stream on finish event handling after document construction ended
 out.on('finish', function() {

  // read pdf file as base64
  fs.readFile('output.pdf', 'base64', function(err, data) {
  if (err) throw err;
  console.log(data);

  // Send base64 pdf to client
    res.status(200).json({
      status: "success",
      data: data,
      message: "Congrats! "
    });
});
});

我使用这里的答案(@CHACO)从其base64-string构造pdf文件客户端:

how to display base64 encoded pdf?

发布评论

评论列表 (0)

  1. 暂无评论