1金币
本帖最后由 Ellia.Duan 于 2023-3-7 14:29 编辑
版本:spredJS 15.2.0
问题:
1、根据提供的例子,使用自定义单元格的方式处理pdf文字显示问题,发现当一个单元格中有多行文字时,转图片会将所有文字转成一行
请问有什么方法保持原格式吗?ssjson文件见附件
- import GC from '@grapecity/spread-sheets';
- import _vue from '@/main';
- function RenderHTMLTagCellType(items, size, isHorizontal) {
- this.typeName = 'RenderHTMLTagCellType';
- this.allowOverflow = false;
- }
- RenderHTMLTagCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
- RenderHTMLTagCellType.prototype.paint = function(
- ctx,
- value,
- x,
- y,
- w,
- h,
- style,
- context
- ) {
- // 自定义单元格什么都不用做,只需要隐藏value即可。
- return GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, [
- ctx,
- ' ',
- x,
- y,
- w,
- h,
- style,
- context,
- ]);
- };
- export function _html2img(spread) {
- try {
- spread.suspendPaint();
- let sheet = spread.getActiveSheet();
- let sels = sheet.getSelections();
- if (!sels || sels.length !== 1) throw new Error('选中区域不合法');
- let range = sels[0];
- for (let r = 0; r < range.rowCount; r++) {
- let row = range.row + r;
- for (let c = 0; c < range.colCount; c++) {
- let col = range.col + c;
- let span = sheet.getSpan(row, col);
- if (span && span.row === row && span.col === col) {
- if (sheet.getText(row, col) && sheet.getText(row, col) !== '') {
- let range = new GC.Spread.Sheets.Range(
- row,
- col,
- span.rowCount,
- span.colCount
- );
- html2ImgByRange(sheet, range);
- }
- }
- if (!span) {
- let range = new GC.Spread.Sheets.Range(row, col, 1, 1);
- html2ImgByRange(sheet, range);
- }
- }
- }
- spread.resumePaint();
- } catch (error) {
- _vue.$alertMessageBox('text2img发生异常!');
- console.log(error);
- }
- }
- function html2ImgByRange(sheet, range) {
- try {
- let value = sheet
- .getRange(range.row, range.col, range.rowCount, range.colCount)
- .toHtml();
- let style = sheet.getActualStyle(range.row, range.col),
- cell = sheet.getCell(range.row, range.col);
- let svgPattern =
- '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
- '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
- let w = cell.sheet.getCellRect(cell.row, cell.col).width;
- let h = cell.sheet.getCellRect(cell.row, cell.col).height;
- let data = svgPattern
- .replace('{0}', w)
- .replace('{1}', h)
- .replace('{2}', style.font)
- .replace('{3}', value);
- let doc = document.implementation.createHTMLDocument('');
- doc.write(data);
- // Get well-formed markup
- data = new XMLSerializer().serializeToString(doc.body.children[0]);
- let img = new Image();
- img.src =
- 'data:image/svg+xml;base64,' +
- window.btoa(unescape(encodeURIComponent(data))); // 涉及中文时的转码
- cell.tag(img);
- sheet.getCell(cell.row, cell.col).backgroundImage(img.src);
- img.onload = (function(cell, value, img) {
- return () => {
- cell.cellType(new RenderHTMLTagCellType());
- cell.value('');
- cell.backgroundImageLayout(GC.Spread.Sheets.ImageLayout.center);
- console.log(img.src);
- cell.backgroundImage(img.src);
- };
- })(cell, value, img);
- } catch (error) {
- console.log(error);
- }
- }
复制代码
2、可不可以通过注册字体,直接解决上下标的问题?比如 arial unicode ms.ttf 字体,纯前端注册字体,发现导出pdf不支持改字体,字体见附件
|
|