云函数调用“office-to-pdf”库,将word的buffer转换成PDF文件,本地调试成功,但真机调试失败,真机调试因为不能看到云函数的本地调试打印,只能看到真机调试时的打印报错如下:
message: "errCode: -401003 api parameter type error | errMsg: parameter.fileID should be string instead of undefined;", errCode: -401003, errMsg: "parameter.fileID should be string instead of undefined;", requestID: , line: 1, …}
调用云函数的js片段如下:
imageToDocx(){
wx.showLoading({
title: '加载中',
})
wx.cloud.callFunction({
// 云函数名称
name: 'imageToDocx',
// 传给云函数的参数
data: {
downloadFileID: this.data.downloadFileID,
//CDN当事人签字图片
signPathLitigantURL: wx.cloud.CDN({
type: 'filePath',
filePath: this.data.signPathLitigant, // 临时文件的路径
}),
//CDN执法人员签字图片
signPathOfficersURL: wx.cloud.CDN({
type: 'filePath',
filePath: this.data.signPathOfficers, // 临时文件的路径
}),
sealURL: this.data.sealURL
},
})
.then(res => {
console.log('云存储文件的ID是:',res)
wx.cloud.downloadFile({
fileID: res.result.fileID,
success: res => {
// 返回并存储临时文件路径
console.log('下载PDF路径:',res.tempFilePath)
this.setData({
exportTempFilePath: res.tempFilePath
})
wx.openDocument({
filePath: res.tempFilePath,
showMenu: 'true',
success: function (res) {
console.log('打开文档成功')
wx.hideLoading()
}
})
},
fail: console.error
})
})
.catch(res=> {
console.log('执行出错:',res)
wx.hideLoading()
if (this.data.previewTempFilePath=='') {
var showModalContent = '未生成预览文书'
} else {
if (typeof(this.data.signPathLitigant)=='undefined' || typeof(this.data.signPathOfficers)=='undefined') {
var showModalContent = '未生成手写签名'
} else {
var showModalContent = '未知错误'
}
}
console.log(showModalContent)
wx.showModal({
title: '导出失败',
showCancel: false,
content: showModalContent,
success (res) {
}
})
})
},
这里再把云函数的代码粘贴如下:
// 云函数入口文件
const cloud = require('wx-server-sdk')
const {
createReport
} = require('docx-templates')
const fs = require('fs')
const path = require('path')
const dayjs = require('dayjs')
const axios = require("axios")
const toPdf = require('office-to-pdf')
cloud.init({
env: 'cloud1-7g1pdoii23b288bf'
})
// 云函数入口函数
exports.main = async (event, context) => {
console.log(event)
const res = await cloud.downloadFile({
fileID: event.downloadFileID,
})
const template = res.fileContent
console.log('当前下载读取文件成功')
//导入当事人签字图片并转码
let signBuffer1 = await axios({
method: 'get',
url: event.signPathLitigantURL,
responseType: 'arraybuffer',
})
const signPathLitigant64 = new Buffer.from(signBuffer1.data, 'binary').toString('base64')
//导入执法人员签字图片并转码
let signBuffer2 = await axios({
method: 'get',
url: event.signPathOfficersURL,
responseType: 'arraybuffer',
headers: { "Content-Type": "*" }
})
const signPathOfficer64 = new Buffer.from(signBuffer2.data, 'binary').toString('base64')
//导入印章图片并转码
let signBuffer3 = await axios({
method: 'get',
url: event.sealURL,
responseType: 'arraybuffer',
headers: { "Content-Type": "*" }
})
const seal64 = new Buffer.from(signBuffer3.data, 'binary').toString('base64')
//开始编译插入
const wordBuffer = await createReport({
template,
data: {
//变量
},
additionalJsContext: {
getMapPicture1: async () => {
const res = signPathLitigant64;
return {
width: 3,
height: 1.6875,
data: res,
extension: '.png',
layout: 'Text wrapping'
};
},
getMapPicture2: async () => {
const res = signPathOfficer64;
return {
width: 3,
height: 1.6875,
data: res,
extension: '.png'
};
},
getMapPicture3: async () => {
const res = seal64;
return {
width: 4,
height: 4,
data: res,
extension: '.png',
layout: 'Text wrapping'
};
},
},
cmdDelimiter: ['+', '+'] //以+作为变量分隔符
})
const preDir = dayjs().format("YYYYMMDD");//日期
console.log('当前的日期是',preDir)
const stringRandom = require('string-random')
const randfilename = stringRandom(32)//随机文件名
const cloudPath = `${preDir}/${randfilename}.pdf`//文件
console.log('当前云路径是',cloudPath)
//开始生成PDF文件
try {
const pdfBuffer = await toPdf(wordBuffer)
console.log('PDF生成成功')
//上传云存储
return await cloud.uploadFile({
cloudPath: cloudPath,
fileContent: Buffer.from(pdfBuffer, 'base64')
})
} catch (error) {
return error
}
}
我的分析是因为云函数中间发生问题,导致没有返回云存储的PDF文件的fileID,所以这样报错,但是因为真机调试时看不到云函数执行中的打印,所以确实找不到问题在哪里,请各位大神指点,在线等,感谢!!!