使用 pypandoc 将 RTF 转换为 PDF 期间页面格式发生变化

问题描述 投票:0回答:1

我正在使用 pypandoc 将 RTF 文件转换为 PDF,但遇到了页面结构和格式在转换过程中发生更改的问题。看起来输出 PDF 是使用 LaTeX 生成的,与原始 RTF 文件相比,这改变了布局。

这是我正在使用的代码:

import pypandoc
def rtf_to_pdf(input_file, output_file):
    """
    Convert an RTF file to PDF using pypandoc.
    
    Args:
    input_file (str): Path to the input RTF file.
    output_file (str): Path where the output PDF will be saved.
    """
    try:
        output = pypandoc.convert_file(input_file, 'pdf', outputfile=output_file)
        print(f"Conversion successful! PDF saved as {output_file}")
        return output
    except Exception as e:
        print(f"An error occurred: {e}")

# Example usage
rtf_to_pdf('input_file.rtf', 'output_file.pdf')

问题在于格式(例如边距、对齐方式、间距)与转换后的原始 RTF 文档不匹配。我只想保留与 RTF 文件相同的格式和布局而不进行任何更改。

问题:

有没有办法使用 pypandoc 或其他库来确保原始 RTF 文件的格式和布局保留在 PDF 输出中? 是否有任何替代方法或库可以用于这种布局保持完全相同的转换?

任何建议或见解将不胜感激!

这是我正在使用的 RTF 文件的简单示例 (sample.rtf):

这是一个测试 rtf 文件到 RTF2XML bean 的示例,用于测试

https://jeroen.github.io/files/sample.rtf

这里是输出的屏幕截图: enter image description here

我正在使用 MicrosoftWord 来可视化 rtf 文档,并且我正在使用 MacOS ,Python 版本:3.11.7,pandoc 版本:3.4

python pdf rtf pdf-conversion pypandoc
1个回答
0
投票

虽然在第一个富文本文件启动之前有 15 行序言,但在您启动手动打印或在 Windows 注册表中设置默认值之前,不会定义新的页面布局。 RTF 的默认设置很难根据打印机笔架宽度等设置左上角和换行。

enter image description here

您可以根据您的区域设置使用 Letter 或 A4 的本机布局。并让本地 RTF 编写器按该比例打印。但边距将取决于之前的设置。

因此,我们可以运行 3 个命令并查看默认的 pdf 格式

curl -O https://jeroen.github.io/files/sample.rtf
write /pt sample.rtf "Microsoft Print to PDF" "Microsoft Print to PDF" sample.pdf
sample.pdf

enter image description here

因此,要选择所需的打印布局,我们需要使用Word或写字板来设置边距和页面大小等。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.