Commit 4572b126 by qinjianhui

Merge branch 'dev' into 'master'

Dev

See merge request !190
parents e32548b9 81fe3f6a
...@@ -1992,14 +1992,35 @@ const printOrder = async ( ...@@ -1992,14 +1992,35 @@ const printOrder = async (
return return
} }
if (data.filePath) { if (data.filePath) {
const strURL = /^https?:/i.test(data.filePath) const strURL = filePath + data.filePath
? data.filePath const isHttpsPdf = /(https):\/\/([^/]+)/i.test(strURL)
: filePath + data.filePath console.log('[printOrder] label source', {
const pdfData = /^https?:/i.test(strURL) id: data.id,
? await downloadPDF(strURL) hasFilePath: !!data.filePath,
: strURL hasFileData: !!data.fileData,
lodop.ADD_PRINT_PDF(0, 0, '100%', '100%', pdfData) 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 { } 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 || '') lodop.SEND_PRINT_RAWDATA(data.fileData || '')
} }
console.log('lodop.CVERSION', lodop.CVERSION) console.log('lodop.CVERSION', lodop.CVERSION)
...@@ -2079,28 +2100,77 @@ const lodopCall = (fn: (lodop: LODOPObject) => string) => { ...@@ -2079,28 +2100,77 @@ const lodopCall = (fn: (lodop: LODOPObject) => string) => {
_lodopCallback[id] = resolve _lodopCallback[id] = resolve
}) })
} }
const downloadPDF = async (url: string): Promise<string> => { const downloadPDF = (url: string) => {
if (!/^https?:/i.test(url)) return url if (!/^https?:/i.test(url)) return url
let xhr,
const headers: Record<string, string> = {} arrybuffer = false,
const token = localStorage.getItem('token') dataArray = null
if (token) headers['jwt-token'] = token if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest()
const response = await fetch(url, { headers }) } else {
if (!response.ok) { xhr = new window.ActiveXObject('MSXML2.XMLHTTP')
console.error(
`PDF download failed: ${response.status} ${response.statusText}`,
)
return ''
} }
xhr.open('GET', url, false) //同步方式
const arrayBuffer = await response.arrayBuffer() if (xhr.overrideMimeType)
const bytes = new Uint8Array(arrayBuffer) try {
let binary = '' xhr.responseType = 'arraybuffer'
for (let i = 0; i < bytes.byteLength; i++) { arrybuffer = true
binary += String.fromCharCode(bytes[i]) } 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)
}
}
} else {
dataArray = window.VBS_BinaryToArray(data).toArray() //兼容IE低版本
} }
return 'data:application/pdf;base64,' + btoa(binary) 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 () => { const handleDownloadMaterial = async () => {
if (!ensureSelection()) return 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