繁体   English   中英

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

[英]React-Native convert a base64 images array to a single pdf

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

好的,我终于在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);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM