OiO.lk Blog python Page format changes during RTF to PDF conversion using pypandoc
python

Page format changes during RTF to PDF conversion using pypandoc


I’m using pypandoc to convert an RTF file to a PDF, but I’m running into an issue where the page structure and formatting are altered during the conversion. It looks like the output PDF is being generated using LaTeX, and this changes the layout compared to the original RTF file.

Here’s the code I’m using:

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')

The issue is that the formatting (e.g., margins, alignment, spacing) does not match the original RTF document after conversion. I just want to retain the same format and layout as the RTF file without any changes.

Question:

Is there a way to use pypandoc or another library to ensure the formatting and layout of the original RTF file is preserved in the PDF output?
Are there any alternative approaches or libraries I can use for this kind of conversion where the layout stays exactly the same?

Any suggestions or insights would be much appreciated!



You need to sign in to view this answers

Exit mobile version