import subprocess subprocess.call(['gs', '-sDEVICE=pdfwrite', '-dCompatibilityLevel=1.4', '-dPDFSETTINGS=/screen', '-dNOPAUSE', '-dQUIET', '-dBATCH', '-sOutputFile=output.pdf', 'input.pdf'])
import base64
with open("output.pdf", "rb") as pdf_file: encoded_pdf = base64.b64encode(pdf_file.read()).decode('utf-8') print(encoded_pdf)
其中,我们先打开输出的PDF文件,然后使用base64.b64encode()函数将其转换为base64格式,并使用decode()函数将其转换为UTF-8编码的字符串。最后,可以将这个字符串写入到文件中,以便后续使用。