Commit 8bfd9fb8 by qinjianhui

fix: 问题修改

parent 1e825855
......@@ -1992,13 +1992,15 @@ const printOrder = async (
return
}
if (data.filePath) {
const strURL = /^https?:/i.test(data.filePath)
? data.filePath
: filePath + data.filePath
const pdfData = /^https?:/i.test(strURL)
? await downloadPDF(strURL)
: strURL
lodop.ADD_PRINT_PDF(0, 0, '100%', '100%', pdfData)
const strURL = filePath + data.filePath
console.log('strURL', strURL, data)
lodop.ADD_PRINT_PDF(
0,
0,
'100%',
'100%',
/(https):\/\/([^/]+)/i.test(strURL) ? downloadPDF(strURL) : strURL,
)
} else {
lodop.SEND_PRINT_RAWDATA(data.fileData || '')
}
......@@ -2079,28 +2081,57 @@ const lodopCall = (fn: (lodop: LODOPObject) => string) => {
_lodopCallback[id] = resolve
})
}
const downloadPDF = async (url: string): Promise<string> => {
const downloadPDF = (url: string) => {
if (!/^https?:/i.test(url)) return url
const headers: Record<string, string> = {}
const token = localStorage.getItem('token')
if (token) headers['jwt-token'] = token
const response = await fetch(url, { headers })
if (!response.ok) {
console.error(
`PDF download failed: ${response.status} ${response.statusText}`,
)
return ''
let xhr,
arrybuffer = false,
dataArray = null
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest()
} else {
xhr = new window.ActiveXObject('MSXML2.XMLHTTP')
}
const arrayBuffer = await response.arrayBuffer()
const bytes = new Uint8Array(arrayBuffer)
let binary = ''
for (let i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i])
xhr.open('GET', url, false) //同步方式
if (xhr.overrideMimeType)
try {
xhr.responseType = 'arraybuffer'
arrybuffer = true
} catch (err) {
xhr.overrideMimeType('text/plain; charset=x-user-defined')
}
xhr.send(null)
const data = xhr.response || xhr.responseBody
if (typeof Uint8Array !== 'undefined') {
if (arrybuffer) {
dataArray = new Uint8Array(data)
} else {
dataArray = new Uint8Array(data.length)
for (let i = 0; i < dataArray.length; i++) {
dataArray[i] = data.charCodeAt(i)
}
}
} else {
dataArray = window.VBS_BinaryToArray(data).toArray() //兼容IE低版本
}
const digits =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
let strData = ''
for (let i = 0, ii = dataArray.length; i < ii; i += 3) {
if (isNaN(dataArray[i])) break
const b1 = dataArray[i] & 0xff,
b2 = dataArray[i + 1] & 0xff,
b3 = dataArray[i + 2] & 0xff
const d1 = b1 >> 2,
d2 = ((b1 & 3) << 4) | (b2 >> 4)
const d3 = i + 1 < ii ? ((b2 & 0xf) << 2) | (b3 >> 6) : 64
const d4 = i + 2 < ii ? b3 & 0x3f : 64
strData +=
digits.substring(d1, d1 + 1) +
digits.substring(d2, d2 + 1) +
digits.substring(d3, d3 + 1) +
digits.substring(d4, d4 + 1)
}
return 'data:application/pdf;base64,' + btoa(binary)
return strData
}
const handleDownloadMaterial = async () => {
if (!ensureSelection()) return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment