异常处理
This commit is contained in:
@@ -110,6 +110,15 @@ def fetch_and_create_pdf(url):
|
||||
# 设置PDF
|
||||
pdf_filename = f"JAV-{today}-{len(today_posts)}.pdf"
|
||||
doc = SimpleDocTemplate(pdf_filename, pagesize=A3)
|
||||
|
||||
# 计算内容区域的宽度和高度
|
||||
page_width, page_height = A3
|
||||
content_width = page_width - doc.rightMargin - doc.leftMargin
|
||||
content_height = page_height - doc.topMargin - doc.bottomMargin
|
||||
|
||||
# 设置最大图片尺寸,留出一些边距
|
||||
max_image_width = content_width * 0.95
|
||||
max_image_height = content_height * 0.7 # 留出足够空间给文本和其他元素
|
||||
|
||||
# 遍历帖子
|
||||
for post in today_posts:
|
||||
@@ -154,48 +163,87 @@ def fetch_and_create_pdf(url):
|
||||
for img_link in image_links:
|
||||
image = download_image(img_link)
|
||||
if image:
|
||||
img = PILImage.open(image)
|
||||
img_width, img_height = img.size
|
||||
image_width = 700
|
||||
image_height = int((img_height / img_width) * image_width)
|
||||
img_stream = BytesIO(image.getvalue())
|
||||
content.append(Image(img_stream, width=image_width, height=image_height))
|
||||
content.append(Spacer(1, 4))
|
||||
try:
|
||||
# 使用PIL处理图片尺寸
|
||||
img = PILImage.open(image)
|
||||
img_width, img_height = img.size
|
||||
|
||||
# 计算缩放比例,确保图片适应页面
|
||||
scale_width = max_image_width / img_width
|
||||
scale_height = max_image_height / img_height
|
||||
scale = min(scale_width, scale_height, 1.0) # 不超过原始大小
|
||||
|
||||
# 计算新的尺寸
|
||||
new_width = img_width * scale
|
||||
new_height = img_height * scale
|
||||
|
||||
# 重置文件指针
|
||||
image.seek(0)
|
||||
img_stream = BytesIO(image.getvalue())
|
||||
|
||||
# 添加图片到内容中,使用计算后的尺寸
|
||||
content.append(Image(img_stream, width=new_width, height=new_height))
|
||||
content.append(Spacer(1, 4))
|
||||
|
||||
print(f"处理图片: 原始尺寸 {img_width}x{img_height}, 新尺寸 {new_width}x{new_height}")
|
||||
except Exception as e:
|
||||
print(f"处理图片时出错: {e}")
|
||||
|
||||
# 在每个帖子后添加分页符(除了最后一页)
|
||||
if post != today_posts[-1]:
|
||||
content.append(PageBreak())
|
||||
|
||||
# 生成PDF
|
||||
doc.build(content)
|
||||
absolute_pdf_path = os.path.abspath(pdf_filename)
|
||||
print(f"PDF saved as {absolute_pdf_path}")
|
||||
try:
|
||||
doc.build(content)
|
||||
absolute_pdf_path = os.path.abspath(pdf_filename)
|
||||
print(f"PDF saved as {absolute_pdf_path}")
|
||||
|
||||
# 加密PDF
|
||||
add_pdf_encryption(absolute_pdf_path)
|
||||
driver.quit()
|
||||
# 加密PDF
|
||||
add_pdf_encryption(absolute_pdf_path)
|
||||
driver.quit()
|
||||
|
||||
return absolute_pdf_path
|
||||
return absolute_pdf_path
|
||||
except Exception as e:
|
||||
print(f"生成PDF时出错: {e}")
|
||||
driver.quit()
|
||||
# 如果生成失败,返回一个默认路径或空字符串
|
||||
return ""
|
||||
|
||||
|
||||
# add_pdf_encryption 和 pdf_file_path 函数保持不变
|
||||
def add_pdf_encryption(pdf_file, password="4000"):
|
||||
"""使用PyPDF2为PDF添加加密保护"""
|
||||
pdf_writer = PdfWriter()
|
||||
pdf_reader = PdfReader(pdf_file)
|
||||
for page_num in range(len(pdf_reader.pages)):
|
||||
pdf_writer.add_page(pdf_reader.pages[page_num])
|
||||
pdf_writer.encrypt(password)
|
||||
with open(pdf_file, "wb") as output_pdf:
|
||||
pdf_writer.write(output_pdf)
|
||||
print(f"PDF加密成功,密码为: {password}")
|
||||
try:
|
||||
pdf_writer = PdfWriter()
|
||||
pdf_reader = PdfReader(pdf_file)
|
||||
for page_num in range(len(pdf_reader.pages)):
|
||||
pdf_writer.add_page(pdf_reader.pages[page_num])
|
||||
pdf_writer.encrypt(password)
|
||||
with open(pdf_file, "wb") as output_pdf:
|
||||
pdf_writer.write(output_pdf)
|
||||
print(f"PDF加密成功,密码为: {password}")
|
||||
except Exception as e:
|
||||
print(f"PDF加密失败: {e}")
|
||||
|
||||
|
||||
def pdf_file_path():
|
||||
url = 'https://www.sehuatang.net/forum.php?mod=forumdisplay&fid=103&filter=typeid&typeid=481'
|
||||
pdf_path = fetch_and_create_pdf(url)
|
||||
print(f"返回的PDF文件路径:{pdf_path}")
|
||||
return pdf_path
|
||||
try:
|
||||
url = 'https://www.sehuatang.net/forum.php?mod=forumdisplay&fid=103&filter=typeid&typeid=481'
|
||||
pdf_path = fetch_and_create_pdf(url)
|
||||
if pdf_path:
|
||||
print(f"返回的PDF文件路径:{pdf_path}")
|
||||
return pdf_path
|
||||
else:
|
||||
# 如果生成失败,返回一个默认的PDF路径
|
||||
default_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "default.pdf")
|
||||
print(f"PDF生成失败,返回默认路径: {default_path}")
|
||||
return default_path
|
||||
except Exception as e:
|
||||
print(f"生成PDF路径时出错: {e}")
|
||||
# 返回一个默认路径
|
||||
default_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "default.pdf")
|
||||
return default_path
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user