Sen Sokha
Sen Sokha

Reputation: 1912

Write content from plain text to pdf file

I have sample content in the link here. it is plain text. How do any solutions to convert this text content to original pdf file? (This content I got from MTOM service)

I got so far from this source multipart/mixed that contain both json and binary content as text.

--uuid:dba94a0e-2d99-4675-9781-2a736995bdc8
Content-Type: application/json;charset=UTF-8
Content-Transfer-Encoding: binary
Content-ID: <jsonInfos>

{"messages":[{"id":"0","type":"INFOS","messageContent":"La requête a été traitée avec succès","replacementValues":[]}]}
--uuid:dba94a0e-2d99-4675-9781-2a736995bdc8
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <label>

%PDF-1.3
%����
12 0 obj
<<
/BitsPerComponent 8
/ColorSpace /DeviceRGB
/Filter [/FlateDecode /DCTDecode]
/Height 80
/Length 2486
/Name /Obj0
/Subtype /Image
/Type /XObject
/Width 119
>>
stream
x���{<��ǟ1f��$1rY�{�QY  �a�Les�-jܧ��Qm��R4!wi&׉�Y�$32��h�1�f�Sg�9�:����y^�?���|���|�5�lr���`0p@:�N)�@"d�H�Bʡ7����h���6��lݪ�������a5t���j��k`h��Hg�  �K}��S
....
....
....
startxref
101943
%%EOF

--uuid:dba94a0e-2d99-4675-9781-2a736995bdc8--

I tried in python:

with open('tmp.txt', 'r') as tmp:
     with open('sample.pdf', 'wb') as sample:
          sample.write(tmp.read().encode('utf-8'))

Upvotes: 1

Views: 3675

Answers (3)

rednafi
rednafi

Reputation: 1731

You cannot get back your original pdf files from plain text files only. Because while exporting to txt, the converter chops of a lot of information like color encoding, structure, font data etc. However, if you just want to create pdf from txt, you can use wkhtmltopdf and pdfkit to achieve that.

  • Install wkhtmltopdf via apt-get install wkhtmltopdf

  • Install pdfkit via pip install pdfkit.

Now you can just do this:

import pdfkit

pdfkit.from_file("tmp.txt", "sample.pdf")

This will return:

libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
Loading page (1/2)
Printing pages (2/2)                                               
Done                                                           
True

The pdf file should look like this:

enter image description here

Upvotes: 0

Anupam Chaplot
Anupam Chaplot

Reputation: 1326

You can consider using FPDF to generate PDF file. PFB the sample code.

from fpdf import FPDF

with open('tmp.txt', 'r') as tmp:
    wpdf = FPDF()
    wpdf.set_font('arial', '', 12)
    wpdf.add_page()
    wpdf.set_xy(10, 5)
    for line in tmp:
        wpdf.cell(50, 5, txt=line, ln=1, align="L")
    wpdf.output('sample.pdf', 'F')`enter code here`


Please refer below link for more information. https://pyfpdf.readthedocs.io/en/latest/Tutorial/index.html

Upvotes: 0

Glyphack
Glyphack

Reputation: 886

You cannot write to pdf files like you write to normal text files. There are libraries in python to write pdf files. you can try pdfrw.

the data you are going to write to pds can have attributes(other than the text you save in text files) follow the samples to do what you actually need:

from pdfrw import PdfWriter
y = PdfWriter()
y.addpage(data)
y.write('result.pdf')

Upvotes: 1

Related Questions