Error converting txt to pdf

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Roark
Posts: 2
Joined: 2018-03-26T16:31:41-07:00
Authentication code: 1152

Error converting txt to pdf

Post by Roark »

I need some help with the most basic of tasks: converting txt to pdf from the command line. I believe that this is the proper code to convert a .txt file to .pdf:

Code: Select all

convert text.txt text.pdf
But I get this error:

Code: Select all

convert: improper image header 'text.txt' @ error/txt.c/ReadTXTImage/446.
convert: no images defined `text.pdf' @ error/convert.c/ConvertImageCommand/3258.
Here is my version info (convert -v):

Code: Select all

Version: ImageMagick 6.9.9-38 Q16 x86_64 2018-03-12 http://www.imagemagick.org
Copyright: © 1999-2018 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP 
Delegates (built-in): bzlib cairo djvu fftw fontconfig freetype gslib jbig jng jp2 jpeg lcms ltdl lzma openexr pangocairo png ps rsvg tiff webp wmf x xml zlib
Do I have the code wrong or is there something else I need to do?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Error converting txt to pdf

Post by snibgo »

To IM, the ".txt" format is a text list of pixel values, one pixel per line, with a header. If you want any arbitrary text file to be rendered as pixels, use the "TEXT:" prefix:

Code: Select all

convert TEXT:text.txt text.pdf
This will make a raster image of each page, and put those inside a PDF file.
snibgo's IM pages: im.snibgo.com
Roark
Posts: 2
Joined: 2018-03-26T16:31:41-07:00
Authentication code: 1152

Re: Error converting txt to pdf

Post by Roark »

Thank you for that! It works.

Is there a way to convert the .txt file so the text is as it appears in the file? Or is that not possible without OCR?

Here is a picture of the .txt file:

Image
http://ibb.co/jvxG5S

Here is a picture of the resulting .pdf:

Image
http://ibb.co/eTvazn

The result from the code above is a little, well, pixely. Also, the margins and justification are different from the original. (Ignore the blue background for the .txt file, that is just my reader settings).

Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Error converting txt to pdf

Post by fmw42 »

If you want to extract the text then you need to do OCR. Imagemagick will only convert to raster format, not ascii text.

What Imagemagick saves to PDF it embeds the raster file in a PDF vector shell. But the default density for the PDF is not the same as that for the raster image. So when viewing the PDF you must tell it a proper density such as 72 dpi to view the same as the original.
Post Reply