pl-slip-pdf-render.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * 运输计划单据(入库单 / 送货单 / 分装单)PDF 截图共用渲染策略。
  3. * html2canvas 对 0.3px 边框与 border-collapse 在 Windows DPI 缩放下易画成粗线。
  4. */
  5. /** 固定像素倍率,不跟 devicePixelRatio,保证 Mac / Windows 栅格化尺度一致 */
  6. export function getPlSlipPdfCanvasScale(): number {
  7. return 3
  8. }
  9. /**
  10. * 将表格单元格边框改为「仅右+底」的 1px 背景线(首行补顶、首列补左)。
  11. * 相邻格只画一条共享边,避免全边框叠成双线。
  12. */
  13. export function applyPdfStableTableCellBorders(cells: Iterable<Element>): void {
  14. const cellList = Array.from(cells) as HTMLElement[]
  15. if (!cellList.length) return
  16. const rows = new Map<HTMLTableRowElement, HTMLElement[]>()
  17. for (const cell of cellList) {
  18. const tr = cell.closest('tr')
  19. if (!tr) continue
  20. const list = rows.get(tr)
  21. if (list) list.push(cell)
  22. else rows.set(tr, [cell])
  23. }
  24. let rowIndex = 0
  25. for (const [, rowCells] of rows) {
  26. rowCells.forEach((cell, colIndex) => {
  27. const gradients = ['linear-gradient(#000,#000)', 'linear-gradient(#000,#000)']
  28. const sizes = ['100% 1px', '1px 100%']
  29. const positions = ['left bottom', 'right top']
  30. if (rowIndex === 0) {
  31. gradients.push('linear-gradient(#000,#000)')
  32. sizes.push('100% 1px')
  33. positions.push('left top')
  34. }
  35. if (colIndex === 0) {
  36. gradients.push('linear-gradient(#000,#000)')
  37. sizes.push('1px 100%')
  38. positions.push('left top')
  39. }
  40. cell.style.setProperty('border', 'none', 'important')
  41. cell.style.setProperty('border-width', '0', 'important')
  42. cell.style.setProperty('background-color', '#ffffff', 'important')
  43. cell.style.setProperty('background-image', gradients.join(','), 'important')
  44. cell.style.setProperty('background-size', sizes.join(','), 'important')
  45. cell.style.setProperty('background-position', positions.join(','), 'important')
  46. cell.style.setProperty('background-repeat', 'no-repeat', 'important')
  47. })
  48. rowIndex += 1
  49. }
  50. }