debug PDF generation

This commit is contained in:
2024-11-08 18:42:07 +09:00
parent 4e4bd7ac5d
commit a0c3a82720
2 changed files with 52 additions and 32 deletions

View File

@ -385,26 +385,34 @@ class SumasenExcel:
except Exception:
cell_height = 20 # デフォルト値
# 最小サイズを設定
cell_width = max(cell_width, 100)
cell_height = max(cell_height, 20)
# 最大サイズを設定
max_width = 800
max_height = 600
cell_width = min(cell_width, max_width)
cell_height = min(cell_height, max_height)
# マージンの設定(ピクセル単位)
margin_horizontal = 4 # 左右のマージン
margin_vertical = 2 # 上下のマージン
# マージンを考慮したセルの実効サイズを計算
effective_cell_width = cell_width - (margin_horizontal * 2)
effective_cell_height = cell_height - (margin_vertical * 2)
# 最小サイズを設定(マージンを考慮)
effective_cell_width = max(effective_cell_width, 92) # 100 - (4 * 2)
effective_cell_height = max(effective_cell_height, 16) # 20 - (2 * 2)
# 最大サイズを設定(マージンを考慮)
max_width = 792 # 800 - (4 * 2)
max_height = 596 # 600 - (2 * 2)
effective_cell_width = min(effective_cell_width, max_width)
effective_cell_height = min(effective_cell_height, max_height)
# アスペクト比を保持しながらリサイズ
img_width, img_height = img.size
img_aspect = img_width / img_height
cell_aspect = cell_width / cell_height
cell_aspect = effective_cell_width / effective_cell_height
if img_aspect > cell_aspect:
width = cell_width
width = effective_cell_width
height = int(width / img_aspect)
else:
height = cell_height
height = effective_cell_height
width = int(height * img_aspect)
# 画像をリサイズ
@ -419,9 +427,21 @@ class SumasenExcel:
from openpyxl.drawing.image import Image as XLImage
excel_image = XLImage(img_byte_arr)
# 画像の配置
excel_image.anchor = f"{column_letter}{cell.row}"
# 画像のオフセット位置を設定(マージンを適用)
from openpyxl.drawing.spreadsheet_drawing import OneCellAnchor, AnchorMarker
from openpyxl.utils.units import pixels_to_EMU
# セルの左上を基準に、マージン分オフセットした位置に配置
col_offset = pixels_to_EMU(margin_horizontal)
row_offset = pixels_to_EMU(margin_vertical)
# マージンを考慮した配置
marker = AnchorMarker(col=cell.column - 1, colOff=col_offset,
row=cell.row - 1, rowOff=row_offset)
anchor = OneCellAnchor(_from=marker, ext=None)
excel_image.anchor = anchor
# 画像をワークシートに追加
worksheet.add_image(excel_image)
@ -432,9 +452,10 @@ class SumasenExcel:
import traceback
logging.error(traceback.format_exc())
return field_value
elif image_path and not os.path.exists(image_path):
logging.warning(f"画像ファイルが存在しません: {image_path}")
return "" #空欄にする
else:
if image_path:
logging.warning(f"画像ファイルが存在しません: {image_path}")
return field_value
except Exception as e: