UnisKB/apps/system_manage/document_export.py

306 lines
16 KiB
Python

"""Structured DOCX authoring and bounded, isolated LibreOffice conversions.
No URL fetching, shell interpolation, macros, shared profiles, or database writes.
"""
import base64
import contextlib
import io
import os
from pathlib import Path
import re
import shutil
import subprocess
import tempfile
import time
from bs4 import BeautifulSoup, NavigableString, Tag
from docx import Document
from docx.enum.text import WD_BREAK
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx.shared import Inches, Pt
from PIL import Image
MAX_HTML = 24 * 1024 * 1024
MAX_IMAGES = 16 * 1024 * 1024
MIMES = {'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'pdf': 'application/pdf', 'rtf': 'application/rtf'}
class ExportError(ValueError):
pass
def _element(parent, name, **attrs):
child = OxmlElement(name)
for key, value in attrs.items():
child.set(qn('w:' + key), str(value))
parent.append(child)
return child
@contextlib.contextmanager
def conversion_slot():
# A machine-wide file lock also bounds work across multiple Web processes.
path = Path(tempfile.gettempdir()) / 'maxkb-document-export.lock'
with path.open('a+b') as stream:
stream.seek(0); stream.write(b'0'); stream.flush(); stream.seek(0)
deadline = time.monotonic() + 15
while True:
try:
if os.name == 'nt':
import msvcrt
msvcrt.locking(stream.fileno(), msvcrt.LK_NBLCK, 1)
else:
import fcntl
fcntl.flock(stream.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
break
except OSError:
if time.monotonic() > deadline:
raise ExportError('导出繁忙,请稍后重试')
time.sleep(.1)
try:
yield
finally:
if os.name == 'nt':
import msvcrt
stream.seek(0); msvcrt.locking(stream.fileno(), msvcrt.LK_UNLCK, 1)
else:
import fcntl
fcntl.flock(stream.fileno(), fcntl.LOCK_UN)
class DocumentBuilder:
def __init__(self, image_resolver=None, paper='A4'):
self.doc = Document()
self.resolve = image_resolver
self.image_bytes = 0
self.image_count = 0
section = self.doc.sections[0]
section.page_width = Inches(8.2677)
section.page_height = Inches(11.6929)
section.top_margin = section.bottom_margin = Inches(.75)
section.left_margin = section.right_margin = Inches(.8)
self.width = section.page_width - section.left_margin - section.right_margin
for name in ['Normal', 'Title', *['Heading ' + str(i) for i in range(1, 7)]]:
style = self.doc.styles[name]
style.font.name = 'Noto Sans CJK SC'
style.font.size = Pt(11 if name == 'Normal' else 22 if name == 'Title' else max(11, 19 - int(name[-1])))
style.font.color.rgb = __import__('docx').shared.RGBColor(0, 0, 0)
fonts = style.element.get_or_add_rPr().rFonts
fonts.set(qn('w:eastAsia'), 'Noto Sans CJK SC')
style.paragraph_format.space_after = Pt(6)
style.paragraph_format.line_spacing = 1.25
style.paragraph_format.widow_control = True
footer = section.footer.paragraphs[0]
footer.alignment = 1
field = _element(footer._p, 'w:fldSimple', instr='PAGE')
_element(_element(field, 'w:r'), 'w:t').text = '1'
def image(self, node, paragraph, width=None):
self.image_count += 1
if self.image_count > 128: raise ExportError('单次导出图片不能超过128张')
src = node.get('src', '')
if src.startswith('data:image/'):
match = re.fullmatch(r'data:image/(png|jpeg|gif|webp|bmp);base64,([A-Za-z0-9+/=\s]+)', src)
if not match:
raise ExportError('图片格式无效')
try:
raw = base64.b64decode(match[2], validate=False)
except (ValueError, TypeError) as exc:
raise ExportError('图片格式无效') from exc
elif self.resolve:
raw = self.resolve(src)
else:
raise ExportError('图片未保存,请重新加载后导出')
self.image_bytes += len(raw)
if self.image_bytes > MAX_IMAGES:
raise ExportError('导出图片总大小不能超过16MB')
try:
with Image.open(io.BytesIO(raw)) as image:
if image.width * image.height > 40_000_000:
raise ExportError('图片尺寸过大')
image.load()
# DOCX/RTF and Office readers share a reliable raster baseline.
converted = io.BytesIO()
image.convert('RGBA' if image.mode == 'RGBA' else 'RGB').save(converted, format='PNG')
natural_width, natural_height = image.size
max_width = min(width or self.width, self.width)
requested_width = str(node.get('width', natural_width))
pixels = int(requested_width) if requested_width.isdigit() and 0 < int(requested_width) < 10000 else natural_width
display_width = min(max_width, Inches(pixels / 96))
display_width = min(display_width, Inches(8.8) * natural_width / max(1, natural_height))
picture = paragraph.add_run().add_picture(io.BytesIO(converted.getvalue()), width=int(display_width))
picture._inline.docPr.set('descr', node.get('alt', ''))
except ExportError:
raise
except Exception as exc:
raise ExportError('图片无法导出,请检查图片格式') from exc
def inline(self, node, paragraph, formats=None, width=None):
formats = dict(formats or {})
if isinstance(node, NavigableString):
run = paragraph.add_run(str(node))
for name in ('bold', 'italic', 'underline', 'strike'):
if formats.get(name):
if name == 'strike': run.font.strike = True
else: setattr(run, name, True)
if formats.get('subscript'): run.font.subscript = True
if formats.get('superscript'): run.font.superscript = True
if formats.get('color'):
from docx.shared import RGBColor
run.font.color.rgb = RGBColor.from_string(formats['color'])
if formats.get('code'):
run.font.name = 'DejaVu Sans Mono'; run.font.size = Pt(9)
return
if not isinstance(node, Tag):
return
tag = node.name.lower()
if tag in ('script', 'style', 'iframe', 'object', 'svg', 'math'):
return
if tag == 'img':
self.image(node, paragraph, width); return
if tag == 'br':
paragraph.add_run().add_break(WD_BREAK.LINE); return
mapping = {'b': 'bold', 'strong': 'bold', 'i': 'italic', 'em': 'italic', 'u': 'underline', 's': 'strike', 'del': 'strike', 'code': 'code', 'sub': 'subscript', 'sup': 'superscript'}
if tag in mapping: formats[mapping[tag]] = True
color = re.search(r'(?:^|;)\s*color\s*:\s*#([0-9a-fA-F]{6})(?:;|$)', node.get('style', ''))
if color: formats['color'] = color[1]
if tag == 'a':
href = node.get('href', '')
if re.match(r'^(https?://|mailto:)', href, re.I):
from docx.opc.constants import RELATIONSHIP_TYPE as RT
hyperlink = _element(paragraph._p, 'w:hyperlink')
hyperlink.set(qn('r:id'), paragraph.part.relate_to(href, RT.HYPERLINK, is_external=True))
# Build styled text with normal runs, then move them into the link.
start = len(paragraph.runs)
for child in node.children: self.inline(child, paragraph, formats, width)
for run in list(paragraph.runs)[start:]: hyperlink.append(run._r)
return
for child in node.children:
self.inline(child, paragraph, formats, width)
def numbering(self, ordered, start=1):
numbering = self.doc.part.numbering_part.element
abstract_id = max([int(n.get(qn('w:abstractNumId'))) for n in numbering.findall(qn('w:abstractNum'))] + [-1]) + 1
abstract = _element(numbering, 'w:abstractNum', abstractNumId=abstract_id)
_element(abstract, 'w:multiLevelType', val='multilevel')
for level in range(9):
lvl = _element(abstract, 'w:lvl', ilvl=level)
_element(lvl, 'w:start', val=start if level == 0 else 1)
_element(lvl, 'w:numFmt', val='decimal' if ordered else 'bullet')
_element(lvl, 'w:lvlText', val=f'%{level + 1}.' if ordered else '•')
_element(_element(lvl, 'w:pPr'), 'w:ind', left=360 * (level + 1), hanging=240)
num_id = max([int(n.get(qn('w:numId'))) for n in numbering.findall(qn('w:num'))] + [0]) + 1
_element(_element(numbering, 'w:num', numId=num_id), 'w:abstractNumId', val=abstract_id)
return num_id
def blocks(self, nodes, container=None, level=0, width=None):
container = self.doc if container is None else container
width = width or self.width
for node in nodes:
if isinstance(node, NavigableString):
if str(node).strip(): container.add_paragraph(str(node))
continue
if not isinstance(node, Tag): continue
tag = node.name.lower()
if tag in ('script', 'style', 'iframe', 'object'): continue
if tag in ('ul', 'ol'):
num_id = self.numbering(tag == 'ol', min(100000, max(1, int(node.get('start', 1)))))
for item in node.find_all('li', recursive=False):
p = container.add_paragraph()
props = _element(p._p.get_or_add_pPr(), 'w:numPr')
_element(props, 'w:ilvl', val=min(level, 8)); _element(props, 'w:numId', val=num_id)
for child in item.children:
if isinstance(child, Tag) and child.name in ('ul', 'ol'):
self.blocks([child], container, level + 1, width)
else: self.inline(child, p, width=width)
continue
if tag == 'table':
rows = node.find_all('tr')
if len(rows) > 2000: raise ExportError('表格行数过多')
occupied = set(); placements = []; columns = 0
for row_index, row in enumerate(rows):
column = 0
for cell in row.find_all(['td', 'th'], recursive=False):
while (row_index, column) in occupied: column += 1
colspan = min(30, max(1, int(cell.get('colspan', 1))))
rowspan = min(len(rows) - row_index, max(1, int(cell.get('rowspan', 1))))
if column + colspan > 30: raise ExportError('表格列数不能超过30列')
for rr in range(row_index, row_index + rowspan):
for cc in range(column, column + colspan):
if (rr, cc) in occupied: raise ExportError('表格合并区域无效')
occupied.add((rr, cc))
placements.append((row_index, column, rowspan, colspan, cell))
column += colspan; columns = max(columns, column)
if not columns: continue
table = container.add_table(rows=len(rows), cols=columns)
table.style = 'Table Grid'; table.autofit = False
for column in table.columns: column.width = int(width / columns)
table_rows = list(table.rows)
# python-docx reconstructs the entire grid on every table.cell()
# call. Cache it once; placement targets never point inside a merged span.
grid = list(table._cells)
for index, row in enumerate(rows):
if row.find('th') is not None: _element(table_rows[index]._tr.get_or_add_trPr(), 'w:tblHeader')
_element(table_rows[index]._tr.get_or_add_trPr(), 'w:cantSplit')
for rr, cc, rowspan, colspan, cell in placements:
target = grid[rr * columns + cc]
if rowspan > 1 or colspan > 1: target = target.merge(grid[(rr + rowspan - 1) * columns + cc + colspan - 1])
p = target.paragraphs[0]
alignment = re.search(r'text-align:\s*(left|center|right)', cell.get('style', ''))
if alignment: p.alignment = {'left':0, 'center':1, 'right':2}[alignment[1]]
for child in cell.children: self.inline(child, p, {'bold': cell.name == 'th'}, int(width / columns * colspan))
container.add_paragraph(); continue
if tag in ('div', 'section', 'article', 'main', 'body', 'blockquote'):
before = len(container.paragraphs)
self.blocks(list(node.children), container, level, width)
if tag == 'blockquote':
for p in container.paragraphs[before:]: p.paragraph_format.left_indent = Inches(.25)
continue
if tag in ('thead', 'tbody', 'tfoot'): continue
p = container.add_paragraph()
if re.fullmatch('h[1-6]', tag):
p.style = 'Heading ' + tag[1]; p.paragraph_format.keep_with_next = True
if tag == 'pre':
p.paragraph_format.space_before = Pt(5)
shading = _element(p._p.get_or_add_pPr(), 'w:shd', fill='F5F7FA')
run = p.add_run(node.get_text()); run.font.name = 'DejaVu Sans Mono'; run.font.size = Pt(9)
elif tag == 'hr':
_element(_element(p._p.get_or_add_pPr(), 'w:pBdr'), 'w:bottom', val='single', sz=4, color='D9DDE4')
else: self.inline(node, p, width=width)
def build(self, html):
soup = BeautifulSoup(html, 'html.parser')
if len(soup.find_all(True)) > 50000: raise ExportError('文档结构过于复杂,请拆分后导出')
self.blocks(list((soup.body or soup).children))
result = io.BytesIO(); self.doc.save(result)
return result.getvalue()
def export_document(html, format_name='docx', image_resolver=None, paper='A4'):
if format_name not in MIMES: raise ExportError('不支持的导出格式')
if paper != 'A4': raise ExportError('仅支持 A4 纸张')
if not isinstance(html, str) or not html.strip() or len(html.encode('utf-8')) > MAX_HTML:
raise ExportError('导出内容为空或超过24MB')
raw = DocumentBuilder(image_resolver, paper).build(html)
if format_name == 'docx': return raw, MIMES[format_name]
command = shutil.which('libreoffice') or shutil.which('soffice')
if command is None: raise ExportError('文档转换组件不可用,请联系管理员或下载 DOCX')
with conversion_slot(), tempfile.TemporaryDirectory(prefix='maxkb-export-') as directory:
root = Path(directory)
(root / 'document.docx').write_bytes(raw)
try:
completed = subprocess.run([command, '-env:UserInstallation=' + (root / 'profile').as_uri(),
'--headless', '--convert-to', format_name, '--outdir', str(root), str(root / 'document.docx')],
stdin=subprocess.DEVNULL, capture_output=True, timeout=90, check=False)
except (OSError, subprocess.TimeoutExpired) as exc:
raise ExportError('文档转换超时或失败,请下载 DOCX 后重试') from exc
result = root / ('document.' + format_name)
if completed.returncode or not result.is_file() or result.stat().st_size == 0:
raise ExportError('文档转换失败,请下载 DOCX 后重试')
output = result.read_bytes()
if format_name == 'pdf' and not output.startswith(b'%PDF-'): raise ExportError('PDF 输出无效')
if format_name == 'rtf' and not output.lstrip().startswith(b'{\\rtf'): raise ExportError('RTF 输出无效')
return output, MIMES[format_name]