Browse Source

预览尺寸和纸张尺寸不一致问题

master
han\hanst 5 months ago
parent
commit
554314d308
  1. 130
      src/views/modules/labelSetting/components/ZPLPreview.vue

130
src/views/modules/labelSetting/components/ZPLPreview.vue

@ -144,10 +144,16 @@ export default {
},
// Labelary APIdpmm
labelaryDpmm() {
// DPIdpmm (dots per millimeter)
// 1 = 25.4 dpmm = dpi / 25.4
const dpmm = Math.round(this.selectedDPI / 25.4)
return dpmm
// Labelary API dpmm 使
const dpiToDpmm = {
152: 6, // 152 DPI = 6 dpmm
203: 8, // 203 DPI = 8 dpmm ()
300: 12, // 300 DPI = 12 dpmm
600: 24 // 600 DPI = 24 dpmm
}
// 使
return dpiToDpmm[this.selectedDPI] || 8 // 使8dpmm (203 DPI)
}
},
watch: {
@ -201,89 +207,45 @@ export default {
methods: {
/**
* 根据纸张类型和方向计算Labelary API需要的尺寸格式
* Labelary API格式: {width}x{height} (单位: 英寸保留1位小数)
* 关键确保尺寸计算的精确性避免预览图尺寸不匹配
*/
getLabelarySize() {
let size
let widthMm, heightMm, paperName = '未知纸张'
// 使ID
// 使ID
if (typeof this.selectedPaper === 'number') {
const paper = dynamicPaperConfig.getPaperById(this.selectedPaper)
if (paper) {
//
size = {
width: Math.round((paper.widthMm / 25.4) * 10) / 10,
height: Math.round((paper.heightMm / 25.4) * 10) / 10
}
widthMm = paper.widthMm
heightMm = paper.heightMm
paperName = paper.name
}
}
//
if (!size) {
//
const paperSizeMap = {
// ( -> )
'40x30': { width: 1.6, height: 1.2 }, // 40mm x 30mm
'50x30': { width: 2.0, height: 1.2 }, // 50mm x 30mm
'60x40': { width: 2.4, height: 1.6 }, // 60mm x 40mm
'80x60': { width: 3.1, height: 2.4 }, // 80mm x 60mm
'100x80': { width: 3.9, height: 3.1 }, // 100mm x 80mm
'100x25': { width: 3.9, height: 1.0 }, // 100mm x 25mm ()
'80x25': { width: 3.1, height: 1.0 }, // 80mm x 25mm ()
'57x30': { width: 2.2, height: 1.2 }, // 57mm x 30mm ()
'80x50': { width: 3.1, height: 2.0 }, // 80mm x 50mm ()
// (使)
'4x6': { width: 4.0, height: 6.0 }, // 4×6
'4x4': { width: 4.0, height: 4.0 }, // 4×4
'4x2': { width: 4.0, height: 2.0 }, // 4×2
'3x2': { width: 3.0, height: 2.0 }, // 3×2
'2x4': { width: 2.0, height: 4.0 }, // 2×4
'2x3': { width: 2.0, height: 3.0 }, // 2×3
'2x1': { width: 2.0, height: 1.0 }, // 2×1
'6x4': { width: 6.0, height: 4.0 } // 6×4
}
if (this.selectedPaper === 'custom') {
//
if (this.canvasSize.widthMm && this.canvasSize.heightMm) {
//
size = {
width: Math.round((this.canvasSize.widthMm / 25.4) * 10) / 10,
height: Math.round((this.canvasSize.heightMm / 25.4) * 10) / 10
}
} else {
// 使 (203DPI)
const dpi = 203
size = {
width: Math.round((this.canvasSize.width / dpi) * 10) / 10,
height: Math.round((this.canvasSize.height / dpi) * 10) / 10
}
}
//
if (!widthMm || !heightMm) {
if (this.canvasSize.widthMm && this.canvasSize.heightMm) {
widthMm = this.canvasSize.widthMm
heightMm = this.canvasSize.heightMm
paperName = this.canvasSize.name || '画布尺寸'
} else {
//
size = paperSizeMap[this.selectedPaper] || paperSizeMap['4x2']
// 使203 DPI
widthMm = Math.round((this.canvasSize.width / 203 * 25.4) * 100) / 100
heightMm = Math.round((this.canvasSize.height / 203 * 25.4) * 100) / 100
paperName = '像素转换'
}
}
//
if (this.orientation === 'landscape') {
const temp = size.width
size.width = size.height
size.height = temp
[widthMm, heightMm] = [heightMm, widthMm]
}
// 2
const widthInch = Math.round((widthMm / 25.4) * 100) / 100
const heightInch = Math.round((heightMm / 25.4) * 100) / 100
// Labelary API
const labelarySize = `${size.width}x${size.height}`
console.log('Labelary尺寸计算:', {
paperType: this.selectedPaper,
orientation: this.orientation,
canvasSize: this.canvasSize,
calculatedSize: size,
labelaryFormat: labelarySize
})
const labelarySize = `${widthInch}x${heightInch}`
return labelarySize
},
@ -302,32 +264,22 @@ export default {
try {
const labelSize = this.getLabelarySize()
const response = await axios.post(
`https://api.labelary.com/v1/printers/${this.labelaryDpmm}dpmm/labels/${labelSize}/0/`,
this.zplCode,
{
responseType: 'blob',
timeout: 10000
const apiUrl = `https://api.labelary.com/v1/printers/${this.labelaryDpmm}dpmm/labels/${labelSize}/0/`
const response = await axios.post(apiUrl, this.zplCode, {
responseType: 'blob',
timeout: 15000, //
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
)
})
// URL
if (this.previewImage) {
URL.revokeObjectURL(this.previewImage)
}
this.previewImage = URL.createObjectURL(response.data)
console.log('预览生成成功:', {
orientation: this.orientation,
paperType: this.selectedPaper,
paperId: typeof this.selectedPaper === 'number' ? this.selectedPaper : null,
canvasSize: this.canvasSize,
elementsCount: this.elements.length
})
} catch (error) {
console.error('预览生成失败:', error)
this.$message.error('预览生成失败,请检查网络连接')
let errorMessage = '预览生成失败'
this.$message.error(errorMessage)
} finally {
this.previewLoading = false
}
@ -352,7 +304,7 @@ export default {
handlePrintSuccess(result) {
console.log('打印成功:', result)
//
}
},
},
beforeDestroy() {

Loading…
Cancel
Save