Advertisement
Python253

bin2pdf

Mar 11th, 2024
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: bin2pdf.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9. This script converts a binary file (.bin) to a PDF file (.pdf).
  10. It reads the binary data from the file and converts it to a string representation.
  11. The binary string is then written to a PDF file with each binary digit in a separate cell.
  12.  
  13. Requirements:
  14. - Python 3.x
  15. - FPDF library (install using: pip install fpdf)
  16.  
  17. Usage:
  18. 1. Save this script as 'bin2pdf.py'.
  19. 2. Ensure your binary file ('example.bin') is in the same directory as the script.
  20. 3. Install the FPDF library using the command: 'pip install fpdf'
  21. 4. Run the script.
  22. 5. The converted PDF file ('bin2pdf.pdf') will be generated in the same directory.
  23.  
  24. Note: Adjust the 'bin_filename' and 'pdf_filename' variables in the script as needed.
  25. """
  26.  
  27. from fpdf import FPDF
  28.  
  29. def bin_to_pdf(bin_filename, pdf_filename):
  30.     with open(bin_filename, 'rb') as binfile:
  31.         binary_data = binfile.read()
  32.  
  33.     pdf = FPDF()
  34.     pdf.add_page()
  35.     pdf.set_font("Arial", size=12)
  36.    
  37.     # Convert binary data to a string representation
  38.     binary_string = ''.join(format(byte, '08b') for byte in binary_data)
  39.    
  40.     # Write binary string to PDF
  41.     pdf.multi_cell(0, 10, binary_string)
  42.  
  43.     pdf.output(pdf_filename)
  44.  
  45. if __name__ == "__main__":
  46.     # Set the filenames for the binary and PDF files
  47.     bin_filename = 'example.bin'
  48.     pdf_filename = 'bin2pdf.pdf'
  49.  
  50.     # Convert the binary file to a PDF file
  51.     bin_to_pdf(bin_filename, pdf_filename)
  52.  
  53.     print(f"Converted '{bin_filename}' to '{pdf_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement