图片到PDF的问题在react native中不起作用



我正在尝试从react native中的图像路径生成PDF,所以我使用下面的插件来实现

https://www.npmjs.com/package/react-native-image-to-pdf/v/1.2.0

根据上面的文档,我配置了所有的东西,下面是我的代码

const myAsyncPDFFunction = async () => {
try {
console.log('Call a');
let path ='file:///Users/macminiharshalk/Library/Developer/CoreSimulator/Devices/FADDF530-05FD-4A0E-9E61-C6AEDB719955/data/Containers/Data/Application/37B8FE42-B23A-4018-865F-F57670B3411E/tmp/606C88B3-5759-4942-A544-1231A0C17532.jpg';
const options = {
imagePaths: [path],
name: 'PDFName',
maxSize: {
// optional maximum image dimension - larger images will be resized
width: 900,
height: Math.round(
(Dimensions.get('window').height / Dimensions.get('window').width) *
900,
),
},
quality: 0.7, // optional compression paramter
//  targetPathRN: "/storage/emulated/0/Download/", // only for android version 9 and lower
//for versions higher than 9 it is stored in (Download/img-to-pdf/)
};
console.log("options-->", options);
const pdf = await RNImageToPdf.createPDFbyImages(options);
console.log('PDF URIs-->', pdf);
console.log(pdf.filePath);
} catch (e) {
console.log(e);
}
};

当我控制台日志,我可以看到pdf路径如下

/Users/macminiharshallk/Library/Developer/CoreSimulator/Devices/FADDF530-05FD-4A0E-9E61-C6AEDB71955/data/Channels/data/Application/37B8FE42-B23A-4018-865F-F57670B3411E/Documents/PDFName.pdf

当我控制台选项参数显示如下

{"imagePaths":[";file:///Users/macminiharshalk/Library/Developer/CoreSimulator/Devices/FADDF530-05FD-4A0E-9E61-C6AEDB719955/data/Containers/Data/Application/37B8FE42-B23A-4018-865F-F57670B3411E/tmp/606C88B3-5759-4942-A544-1231A0C17532.jpg"]"maxSize":{高度:1948,宽度:900};name":"PDFName""质量":0.7}

但当我打开PDF时,图像不是副本,它是空白的PDF,所以知道我如何在PDF中显示图像吗?

请尝试const newPath=path.replace('file://',​​'');

最新更新