Commit 4572b126 by qinjianhui

Merge branch 'dev' into 'master'

Dev

See merge request !190
parents e32548b9 81fe3f6a
......@@ -1992,14 +1992,35 @@ 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
const isHttpsPdf = /(https):\/\/([^/]+)/i.test(strURL)
console.log('[printOrder] label source', {
id: data.id,
hasFilePath: !!data.filePath,
hasFileData: !!data.fileData,
isHttpsPdf,
strURL,
})
const pdfPayload = isHttpsPdf ? downloadPDF(strURL) : strURL
console.log('[printOrder] pdf payload meta', {
mode: isHttpsPdf ? 'base64' : 'url',
length: pdfPayload?.length || 0,
preview: pdfPayload?.slice(0, 30),
})
lodop.ADD_PRINT_PDF(
0,
0,
'100%',
'100%',
pdfPayload,
)
} else {
console.log('[printOrder] raw payload meta', {
id: data.id,
hasFilePath: !!data.filePath,
fileDataLength: data.fileData?.length || 0,
fileDataPreview: data.fileData?.slice(0, 30),
})
lodop.SEND_PRINT_RAWDATA(data.fileData || '')
}
console.log('lodop.CVERSION', lodop.CVERSION)
......@@ -2079,28 +2100,77 @@ 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')
}
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)
console.log('[downloadPDF] response meta', {
url,
status: xhr.status,
readyState: xhr.readyState,
contentType: xhr.getResponseHeader?.('content-type'),
responseType: xhr.responseType,
})
const data = xhr.response || xhr.responseBody
const dataByteLength = arrybuffer
? (data as ArrayBuffer | null)?.byteLength || 0
: (data as string | null)?.length || 0
console.log('[downloadPDF] data length', {
url,
arrybuffer,
dataByteLength,
})
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)
}
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])
}
return 'data:application/pdf;base64,' + btoa(binary)
} 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)
}
console.log('[downloadPDF] base64 meta', {
url,
base64Length: strData.length,
base64Preview: strData.slice(0, 30),
})
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