React-Native将base64图像数组转换为单个pdf

时间:2017-09-29 08:47:16

标签: image pdf react-native base64

我正在使用react-native / redux中的移动应用程序,我必须将一系列base64图像转换为pdf,以便将pdf发送到后端。 关于如何实现它的任何想法?

1 个答案:

答案 0 :(得分:2)

好的,我终于在react-native-image-to-pdf的帮助下找到了一个简单的解决方案 这是promis的基础。在我称为“pdfConverter.js”的文件中,我创建了这个函数

import RNImageToPdf from "react-native-image-to-pdf";

export default base64Arr => {
  // It is a promise based function
  // Create an array containing the path of each base64 images
  let base64Paths = [];
  base64Paths.length = 0; // re-initialize the array for further re-use

  base64Arr.forEach(base64 => {
    base64Paths.push(`data:image/jpeg;base64,${base64}`);
  });

  // Convert base64 images to pdf from the paths array
  return RNImageToPdf.createPDFbyImages({
    imagePaths: base64Paths,
    name: "PDF_Name"
  });
};

然后在另一个文件中将其调用到我需要的位置:

import toPDF from "./pdfConverter.js";

toPDF(myBase64array)
.then(pdf => {
  console.log("pdf ", pdf);
});