latex_toolbox.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. import os, shutil
  2. import re
  3. import numpy as np
  4. PRESERVE = 0
  5. TRANSFORM = 1
  6. pj = os.path.join
  7. class LinkedListNode():
  8. """
  9. Linked List Node
  10. """
  11. def __init__(self, string, preserve=True) -> None:
  12. self.string = string
  13. self.preserve = preserve
  14. self.next = None
  15. self.range = None
  16. # self.begin_line = 0
  17. # self.begin_char = 0
  18. def convert_to_linklist(text, mask):
  19. root = LinkedListNode("", preserve=True)
  20. current_node = root
  21. for c, m, i in zip(text, mask, range(len(text))):
  22. if (m==PRESERVE and current_node.preserve) \
  23. or (m==TRANSFORM and not current_node.preserve):
  24. # add
  25. current_node.string += c
  26. else:
  27. current_node.next = LinkedListNode(c, preserve=(m==PRESERVE))
  28. current_node = current_node.next
  29. return root
  30. def post_process(root):
  31. # 修复括号
  32. node = root
  33. while True:
  34. string = node.string
  35. if node.preserve:
  36. node = node.next
  37. if node is None: break
  38. continue
  39. def break_check(string):
  40. str_stack = [""] # (lv, index)
  41. for i, c in enumerate(string):
  42. if c == '{':
  43. str_stack.append('{')
  44. elif c == '}':
  45. if len(str_stack) == 1:
  46. print('stack fix')
  47. return i
  48. str_stack.pop(-1)
  49. else:
  50. str_stack[-1] += c
  51. return -1
  52. bp = break_check(string)
  53. if bp == -1:
  54. pass
  55. elif bp == 0:
  56. node.string = string[:1]
  57. q = LinkedListNode(string[1:], False)
  58. q.next = node.next
  59. node.next = q
  60. else:
  61. node.string = string[:bp]
  62. q = LinkedListNode(string[bp:], False)
  63. q.next = node.next
  64. node.next = q
  65. node = node.next
  66. if node is None: break
  67. # 屏蔽空行和太短的句子
  68. node = root
  69. while True:
  70. if len(node.string.strip('\n').strip(''))==0: node.preserve = True
  71. if len(node.string.strip('\n').strip(''))<42: node.preserve = True
  72. node = node.next
  73. if node is None: break
  74. node = root
  75. while True:
  76. if node.next and node.preserve and node.next.preserve:
  77. node.string += node.next.string
  78. node.next = node.next.next
  79. node = node.next
  80. if node is None: break
  81. # 将前后断行符脱离
  82. node = root
  83. prev_node = None
  84. while True:
  85. if not node.preserve:
  86. lstriped_ = node.string.lstrip().lstrip('\n')
  87. if (prev_node is not None) and (prev_node.preserve) and (len(lstriped_)!=len(node.string)):
  88. prev_node.string += node.string[:-len(lstriped_)]
  89. node.string = lstriped_
  90. rstriped_ = node.string.rstrip().rstrip('\n')
  91. if (node.next is not None) and (node.next.preserve) and (len(rstriped_)!=len(node.string)):
  92. node.next.string = node.string[len(rstriped_):] + node.next.string
  93. node.string = rstriped_
  94. # =====
  95. prev_node = node
  96. node = node.next
  97. if node is None: break
  98. # 标注节点的行数范围
  99. node = root
  100. n_line = 0
  101. expansion = 2
  102. while True:
  103. n_l = node.string.count('\n')
  104. node.range = [n_line-expansion, n_line+n_l+expansion] # 失败时,扭转的范围
  105. n_line = n_line+n_l
  106. node = node.next
  107. if node is None: break
  108. return root
  109. """
  110. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  111. Latex segmentation with a binary mask (PRESERVE=0, TRANSFORM=1)
  112. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  113. """
  114. def set_forbidden_text(text, mask, pattern, flags=0):
  115. """
  116. Add a preserve text area in this paper
  117. e.g. with pattern = r"\\begin\{algorithm\}(.*?)\\end\{algorithm\}"
  118. you can mask out (mask = PRESERVE so that text become untouchable for GPT)
  119. everything between "\begin{equation}" and "\end{equation}"
  120. """
  121. if isinstance(pattern, list): pattern = '|'.join(pattern)
  122. pattern_compile = re.compile(pattern, flags)
  123. for res in pattern_compile.finditer(text):
  124. mask[res.span()[0]:res.span()[1]] = PRESERVE
  125. return text, mask
  126. def reverse_forbidden_text(text, mask, pattern, flags=0, forbid_wrapper=True):
  127. """
  128. Move area out of preserve area (make text editable for GPT)
  129. count the number of the braces so as to catch compelete text area.
  130. e.g.
  131. \begin{abstract} blablablablablabla. \end{abstract}
  132. """
  133. if isinstance(pattern, list): pattern = '|'.join(pattern)
  134. pattern_compile = re.compile(pattern, flags)
  135. for res in pattern_compile.finditer(text):
  136. if not forbid_wrapper:
  137. mask[res.span()[0]:res.span()[1]] = TRANSFORM
  138. else:
  139. mask[res.regs[0][0]: res.regs[1][0]] = PRESERVE # '\\begin{abstract}'
  140. mask[res.regs[1][0]: res.regs[1][1]] = TRANSFORM # abstract
  141. mask[res.regs[1][1]: res.regs[0][1]] = PRESERVE # abstract
  142. return text, mask
  143. def set_forbidden_text_careful_brace(text, mask, pattern, flags=0):
  144. """
  145. Add a preserve text area in this paper (text become untouchable for GPT).
  146. count the number of the braces so as to catch compelete text area.
  147. e.g.
  148. \caption{blablablablabla\texbf{blablabla}blablabla.}
  149. """
  150. pattern_compile = re.compile(pattern, flags)
  151. for res in pattern_compile.finditer(text):
  152. brace_level = -1
  153. p = begin = end = res.regs[0][0]
  154. for _ in range(1024*16):
  155. if text[p] == '}' and brace_level == 0: break
  156. elif text[p] == '}': brace_level -= 1
  157. elif text[p] == '{': brace_level += 1
  158. p += 1
  159. end = p+1
  160. mask[begin:end] = PRESERVE
  161. return text, mask
  162. def reverse_forbidden_text_careful_brace(text, mask, pattern, flags=0, forbid_wrapper=True):
  163. """
  164. Move area out of preserve area (make text editable for GPT)
  165. count the number of the braces so as to catch compelete text area.
  166. e.g.
  167. \caption{blablablablabla\texbf{blablabla}blablabla.}
  168. """
  169. pattern_compile = re.compile(pattern, flags)
  170. for res in pattern_compile.finditer(text):
  171. brace_level = 0
  172. p = begin = end = res.regs[1][0]
  173. for _ in range(1024*16):
  174. if text[p] == '}' and brace_level == 0: break
  175. elif text[p] == '}': brace_level -= 1
  176. elif text[p] == '{': brace_level += 1
  177. p += 1
  178. end = p
  179. mask[begin:end] = TRANSFORM
  180. if forbid_wrapper:
  181. mask[res.regs[0][0]:begin] = PRESERVE
  182. mask[end:res.regs[0][1]] = PRESERVE
  183. return text, mask
  184. def set_forbidden_text_begin_end(text, mask, pattern, flags=0, limit_n_lines=42):
  185. """
  186. Find all \begin{} ... \end{} text block that with less than limit_n_lines lines.
  187. Add it to preserve area
  188. """
  189. pattern_compile = re.compile(pattern, flags)
  190. def search_with_line_limit(text, mask):
  191. for res in pattern_compile.finditer(text):
  192. cmd = res.group(1) # begin{what}
  193. this = res.group(2) # content between begin and end
  194. this_mask = mask[res.regs[2][0]:res.regs[2][1]]
  195. white_list = ['document', 'abstract', 'lemma', 'definition', 'sproof',
  196. 'em', 'emph', 'textit', 'textbf', 'itemize', 'enumerate']
  197. if (cmd in white_list) or this.count('\n') >= limit_n_lines: # use a magical number 42
  198. this, this_mask = search_with_line_limit(this, this_mask)
  199. mask[res.regs[2][0]:res.regs[2][1]] = this_mask
  200. else:
  201. mask[res.regs[0][0]:res.regs[0][1]] = PRESERVE
  202. return text, mask
  203. return search_with_line_limit(text, mask)
  204. """
  205. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  206. Latex Merge File
  207. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  208. """
  209. def find_main_tex_file(file_manifest, mode):
  210. """
  211. 在多Tex文档中,寻找主文件,必须包含documentclass,返回找到的第一个。
  212. P.S. 但愿没人把latex模板放在里面传进来 (6.25 加入判定latex模板的代码)
  213. """
  214. canidates = []
  215. for texf in file_manifest:
  216. if os.path.basename(texf).startswith('merge'):
  217. continue
  218. with open(texf, 'r', encoding='utf8', errors='ignore') as f:
  219. file_content = f.read()
  220. if r'\documentclass' in file_content:
  221. canidates.append(texf)
  222. else:
  223. continue
  224. if len(canidates) == 0:
  225. raise RuntimeError('无法找到一个主Tex文件(包含documentclass关键字)')
  226. elif len(canidates) == 1:
  227. return canidates[0]
  228. else: # if len(canidates) >= 2 通过一些Latex模板中常见(但通常不会出现在正文)的单词,对不同latex源文件扣分,取评分最高者返回
  229. canidates_score = []
  230. # 给出一些判定模板文档的词作为扣分项
  231. unexpected_words = ['\\LaTeX', 'manuscript', 'Guidelines', 'font', 'citations', 'rejected', 'blind review', 'reviewers']
  232. expected_words = ['\\input', '\\ref', '\\cite']
  233. for texf in canidates:
  234. canidates_score.append(0)
  235. with open(texf, 'r', encoding='utf8', errors='ignore') as f:
  236. file_content = f.read()
  237. file_content = rm_comments(file_content)
  238. for uw in unexpected_words:
  239. if uw in file_content:
  240. canidates_score[-1] -= 1
  241. for uw in expected_words:
  242. if uw in file_content:
  243. canidates_score[-1] += 1
  244. select = np.argmax(canidates_score) # 取评分最高者返回
  245. return canidates[select]
  246. def rm_comments(main_file):
  247. new_file_remove_comment_lines = []
  248. for l in main_file.splitlines():
  249. # 删除整行的空注释
  250. if l.lstrip().startswith("%"):
  251. pass
  252. else:
  253. new_file_remove_comment_lines.append(l)
  254. main_file = '\n'.join(new_file_remove_comment_lines)
  255. # main_file = re.sub(r"\\include{(.*?)}", r"\\input{\1}", main_file) # 将 \include 命令转换为 \input 命令
  256. main_file = re.sub(r'(?<!\\)%.*', '', main_file) # 使用正则表达式查找半行注释, 并替换为空字符串
  257. return main_file
  258. def find_tex_file_ignore_case(fp):
  259. dir_name = os.path.dirname(fp)
  260. base_name = os.path.basename(fp)
  261. # 如果输入的文件路径是正确的
  262. if os.path.isfile(pj(dir_name, base_name)): return pj(dir_name, base_name)
  263. # 如果不正确,试着加上.tex后缀试试
  264. if not base_name.endswith('.tex'): base_name+='.tex'
  265. if os.path.isfile(pj(dir_name, base_name)): return pj(dir_name, base_name)
  266. # 如果还找不到,解除大小写限制,再试一次
  267. import glob
  268. for f in glob.glob(dir_name+'/*.tex'):
  269. base_name_s = os.path.basename(fp)
  270. base_name_f = os.path.basename(f)
  271. if base_name_s.lower() == base_name_f.lower(): return f
  272. # 试着加上.tex后缀试试
  273. if not base_name_s.endswith('.tex'): base_name_s+='.tex'
  274. if base_name_s.lower() == base_name_f.lower(): return f
  275. return None
  276. def merge_tex_files_(project_foler, main_file, mode):
  277. """
  278. Merge Tex project recrusively
  279. """
  280. main_file = rm_comments(main_file)
  281. for s in reversed([q for q in re.finditer(r"\\input\{(.*?)\}", main_file, re.M)]):
  282. f = s.group(1)
  283. fp = os.path.join(project_foler, f)
  284. fp_ = find_tex_file_ignore_case(fp)
  285. if fp_:
  286. try:
  287. with open(fp_, 'r', encoding='utf-8', errors='replace') as fx: c = fx.read()
  288. except:
  289. c = f"\n\nWarning from GPT-Academic: LaTex source file is missing!\n\n"
  290. else:
  291. raise RuntimeError(f'找不到{fp},Tex源文件缺失!')
  292. c = merge_tex_files_(project_foler, c, mode)
  293. main_file = main_file[:s.span()[0]] + c + main_file[s.span()[1]:]
  294. return main_file
  295. def find_title_and_abs(main_file):
  296. def extract_abstract_1(text):
  297. pattern = r"\\abstract\{(.*?)\}"
  298. match = re.search(pattern, text, re.DOTALL)
  299. if match:
  300. return match.group(1)
  301. else:
  302. return None
  303. def extract_abstract_2(text):
  304. pattern = r"\\begin\{abstract\}(.*?)\\end\{abstract\}"
  305. match = re.search(pattern, text, re.DOTALL)
  306. if match:
  307. return match.group(1)
  308. else:
  309. return None
  310. def extract_title(string):
  311. pattern = r"\\title\{(.*?)\}"
  312. match = re.search(pattern, string, re.DOTALL)
  313. if match:
  314. return match.group(1)
  315. else:
  316. return None
  317. abstract = extract_abstract_1(main_file)
  318. if abstract is None:
  319. abstract = extract_abstract_2(main_file)
  320. title = extract_title(main_file)
  321. return title, abstract
  322. def merge_tex_files(project_foler, main_file, mode):
  323. """
  324. Merge Tex project recrusively
  325. P.S. 顺便把CTEX塞进去以支持中文
  326. P.S. 顺便把Latex的注释去除
  327. """
  328. main_file = merge_tex_files_(project_foler, main_file, mode)
  329. main_file = rm_comments(main_file)
  330. if mode == 'translate_zh':
  331. # find paper documentclass
  332. pattern = re.compile(r'\\documentclass.*\n')
  333. match = pattern.search(main_file)
  334. assert match is not None, "Cannot find documentclass statement!"
  335. position = match.end()
  336. add_ctex = '\\usepackage{ctex}\n'
  337. add_url = '\\usepackage{url}\n' if '{url}' not in main_file else ''
  338. main_file = main_file[:position] + add_ctex + add_url + main_file[position:]
  339. # fontset=windows
  340. import platform
  341. main_file = re.sub(r"\\documentclass\[(.*?)\]{(.*?)}", r"\\documentclass[\1,fontset=windows,UTF8]{\2}",main_file)
  342. main_file = re.sub(r"\\documentclass{(.*?)}", r"\\documentclass[fontset=windows,UTF8]{\1}",main_file)
  343. # find paper abstract
  344. pattern_opt1 = re.compile(r'\\begin\{abstract\}.*\n')
  345. pattern_opt2 = re.compile(r"\\abstract\{(.*?)\}", flags=re.DOTALL)
  346. match_opt1 = pattern_opt1.search(main_file)
  347. match_opt2 = pattern_opt2.search(main_file)
  348. if (match_opt1 is None) and (match_opt2 is None):
  349. # "Cannot find paper abstract section!"
  350. main_file = insert_abstract(main_file)
  351. match_opt1 = pattern_opt1.search(main_file)
  352. match_opt2 = pattern_opt2.search(main_file)
  353. assert (match_opt1 is not None) or (match_opt2 is not None), "Cannot find paper abstract section!"
  354. return main_file
  355. insert_missing_abs_str = r"""
  356. \begin{abstract}
  357. The GPT-Academic program cannot find abstract section in this paper.
  358. \end{abstract}
  359. """
  360. def insert_abstract(tex_content):
  361. if "\\maketitle" in tex_content:
  362. # find the position of "\maketitle"
  363. find_index = tex_content.index("\\maketitle")
  364. # find the nearest ending line
  365. end_line_index = tex_content.find("\n", find_index)
  366. # insert "abs_str" on the next line
  367. modified_tex = tex_content[:end_line_index+1] + '\n\n' + insert_missing_abs_str + '\n\n' + tex_content[end_line_index+1:]
  368. return modified_tex
  369. elif r"\begin{document}" in tex_content:
  370. # find the position of "\maketitle"
  371. find_index = tex_content.index(r"\begin{document}")
  372. # find the nearest ending line
  373. end_line_index = tex_content.find("\n", find_index)
  374. # insert "abs_str" on the next line
  375. modified_tex = tex_content[:end_line_index+1] + '\n\n' + insert_missing_abs_str + '\n\n' + tex_content[end_line_index+1:]
  376. return modified_tex
  377. else:
  378. return tex_content
  379. """
  380. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  381. Post process
  382. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  383. """
  384. def mod_inbraket(match):
  385. """
  386. 为啥chatgpt会把cite里面的逗号换成中文逗号呀
  387. """
  388. # get the matched string
  389. cmd = match.group(1)
  390. str_to_modify = match.group(2)
  391. # modify the matched string
  392. str_to_modify = str_to_modify.replace(':', ':') # 前面是中文冒号,后面是英文冒号
  393. str_to_modify = str_to_modify.replace(',', ',') # 前面是中文逗号,后面是英文逗号
  394. # str_to_modify = 'BOOM'
  395. return "\\" + cmd + "{" + str_to_modify + "}"
  396. def fix_content(final_tex, node_string):
  397. """
  398. Fix common GPT errors to increase success rate
  399. """
  400. final_tex = re.sub(r"(?<!\\)%", "\\%", final_tex)
  401. final_tex = re.sub(r"\\([a-z]{2,10})\ \{", r"\\\1{", string=final_tex)
  402. final_tex = re.sub(r"\\\ ([a-z]{2,10})\{", r"\\\1{", string=final_tex)
  403. final_tex = re.sub(r"\\([a-z]{2,10})\{([^\}]*?)\}", mod_inbraket, string=final_tex)
  404. if "Traceback" in final_tex and "[Local Message]" in final_tex:
  405. final_tex = node_string # 出问题了,还原原文
  406. if node_string.count('\\begin') != final_tex.count('\\begin'):
  407. final_tex = node_string # 出问题了,还原原文
  408. if node_string.count('\_') > 0 and node_string.count('\_') > final_tex.count('\_'):
  409. # walk and replace any _ without \
  410. final_tex = re.sub(r"(?<!\\)_", "\\_", final_tex)
  411. def compute_brace_level(string):
  412. # this function count the number of { and }
  413. brace_level = 0
  414. for c in string:
  415. if c == "{": brace_level += 1
  416. elif c == "}": brace_level -= 1
  417. return brace_level
  418. def join_most(tex_t, tex_o):
  419. # this function join translated string and original string when something goes wrong
  420. p_t = 0
  421. p_o = 0
  422. def find_next(string, chars, begin):
  423. p = begin
  424. while p < len(string):
  425. if string[p] in chars: return p, string[p]
  426. p += 1
  427. return None, None
  428. while True:
  429. res1, char = find_next(tex_o, ['{','}'], p_o)
  430. if res1 is None: break
  431. res2, char = find_next(tex_t, [char], p_t)
  432. if res2 is None: break
  433. p_o = res1 + 1
  434. p_t = res2 + 1
  435. return tex_t[:p_t] + tex_o[p_o:]
  436. if compute_brace_level(final_tex) != compute_brace_level(node_string):
  437. # 出问题了,还原部分原文,保证括号正确
  438. final_tex = join_most(final_tex, node_string)
  439. return final_tex
  440. def compile_latex_with_timeout(command, cwd, timeout=60):
  441. import subprocess
  442. process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)
  443. try:
  444. stdout, stderr = process.communicate(timeout=timeout)
  445. except subprocess.TimeoutExpired:
  446. process.kill()
  447. stdout, stderr = process.communicate()
  448. print("Process timed out!")
  449. return False
  450. return True
  451. def run_in_subprocess_wrapper_func(func, args, kwargs, return_dict, exception_dict):
  452. import sys
  453. try:
  454. result = func(*args, **kwargs)
  455. return_dict['result'] = result
  456. except Exception as e:
  457. exc_info = sys.exc_info()
  458. exception_dict['exception'] = exc_info
  459. def run_in_subprocess(func):
  460. import multiprocessing
  461. def wrapper(*args, **kwargs):
  462. return_dict = multiprocessing.Manager().dict()
  463. exception_dict = multiprocessing.Manager().dict()
  464. process = multiprocessing.Process(target=run_in_subprocess_wrapper_func,
  465. args=(func, args, kwargs, return_dict, exception_dict))
  466. process.start()
  467. process.join()
  468. process.close()
  469. if 'exception' in exception_dict:
  470. # ooops, the subprocess ran into an exception
  471. exc_info = exception_dict['exception']
  472. raise exc_info[1].with_traceback(exc_info[2])
  473. if 'result' in return_dict.keys():
  474. # If the subprocess ran successfully, return the result
  475. return return_dict['result']
  476. return wrapper
  477. def _merge_pdfs(pdf1_path, pdf2_path, output_path):
  478. import PyPDF2 # PyPDF2这个库有严重的内存泄露问题,把它放到子进程中运行,从而方便内存的释放
  479. Percent = 0.95
  480. # raise RuntimeError('PyPDF2 has a serious memory leak problem, please use other tools to merge PDF files.')
  481. # Open the first PDF file
  482. with open(pdf1_path, 'rb') as pdf1_file:
  483. pdf1_reader = PyPDF2.PdfFileReader(pdf1_file)
  484. # Open the second PDF file
  485. with open(pdf2_path, 'rb') as pdf2_file:
  486. pdf2_reader = PyPDF2.PdfFileReader(pdf2_file)
  487. # Create a new PDF file to store the merged pages
  488. output_writer = PyPDF2.PdfFileWriter()
  489. # Determine the number of pages in each PDF file
  490. num_pages = max(pdf1_reader.numPages, pdf2_reader.numPages)
  491. # Merge the pages from the two PDF files
  492. for page_num in range(num_pages):
  493. # Add the page from the first PDF file
  494. if page_num < pdf1_reader.numPages:
  495. page1 = pdf1_reader.getPage(page_num)
  496. else:
  497. page1 = PyPDF2.PageObject.createBlankPage(pdf1_reader)
  498. # Add the page from the second PDF file
  499. if page_num < pdf2_reader.numPages:
  500. page2 = pdf2_reader.getPage(page_num)
  501. else:
  502. page2 = PyPDF2.PageObject.createBlankPage(pdf1_reader)
  503. # Create a new empty page with double width
  504. new_page = PyPDF2.PageObject.createBlankPage(
  505. width = int(int(page1.mediaBox.getWidth()) + int(page2.mediaBox.getWidth()) * Percent),
  506. height = max(page1.mediaBox.getHeight(), page2.mediaBox.getHeight())
  507. )
  508. new_page.mergeTranslatedPage(page1, 0, 0)
  509. new_page.mergeTranslatedPage(page2, int(int(page1.mediaBox.getWidth())-int(page2.mediaBox.getWidth())* (1-Percent)), 0)
  510. output_writer.addPage(new_page)
  511. # Save the merged PDF file
  512. with open(output_path, 'wb') as output_file:
  513. output_writer.write(output_file)
  514. merge_pdfs = run_in_subprocess(_merge_pdfs) # PyPDF2这个库有严重的内存泄露问题,把它放到子进程中运行,从而方便内存的释放