#!/bin/bash # Create a PDF file from a JPEG file losslessly # This works by creating on the fly a PDF file with just one picture and # resizing the page to the size of the picture. # jpg2pdf file.jpg basedir="$(dirname "$0")" infile="$1" outfile="$(dirname "$infile")/$(basename "$(basename "$infile" .jpg)" .JPG ).pdf" LENGTH=$(stat -c "%s" "$infile") #HEIGHT=$(exif -t 0xa003 "$infile" | grep Value | sed -r "s/.*: ([[:digit:]]+)/\1/") HEIGHT=$(identify -format "%[fx:h]" "$infile") #WIDTH=$(exif -t 0xa002 "$infile" | grep Value | sed -r "s/.*: ([[:digit:]]+)/\1/") WIDTH=$(identify -format "%[fx:w]" "$infile") #DPI=300 # $(exif -t 011a "$infile" | grep Value | sed -r "s/.*: ([[:digit:]]+)/\1/") DPI=$(identify -format "%x" "$infile" | sed -r "s/([[:digit:]]+).*/\1/") COLORSPACE=$(identify -format "%r" "$infile") BPC=$(identify -format "%q" "$infile") # or %z? # same in user unit (1/72 of inch) HEIGHTU=$(echo "scale=5; ($HEIGHT * 72) / $DPI" | bc) WIDTHU=$(echo "scale=5; ($WIDTH * 72) / $DPI" | bc) # Need to handle also the colorspace : /DeviceGray /DeviceCMYK /DeviceRGB # Need to handle the bit-per components # Put the header #sed -r "s/LENGTH/$LENGTH/; s/HEIGHT/$HEIGHT/; s/WIDTH/$WIDTH/" "$basedir/jpg2pdf.header" > "$outfile"u # Put the picture cat - "$infile" > "$outfile"u << ENDHEADER %PDF-1.4 %âãÏÓ 1 0 obj << /ColorSpace /DeviceRGB /Subtype /Image /Filter /DCTDecode /Length $LENGTH /Width $WIDTH /Type /XObject /Height $HEIGHT /BitsPerComponent 8 >> stream ENDHEADER # Put the footer #sed -r "s/HEIGHTU/$HEIGHTU/; s/WIDTHU/$WIDTHU/" "$basedir/jpg2pdf.footer" >> "$outfile"u cat - >> "$outfile"u << ENDFOOTER endstream endobj 2 0 obj << /XObject << /Im1 1 0 R >> /ProcSet [/PDF /ImageC] >> endobj 3 0 obj << /Parent 4 0 R /MediaBox [0 0 $WIDTHU $HEIGHTU] /Resources 2 0 R /pdftk_PageNum 1 /Contents 5 0 R /Type /Page >> endobj 5 0 obj << /Length 90 >> stream $WIDTHU 0 0 $HEIGHTU 0 0 cm /Im1 Do endstream endobj 4 0 obj << /Kids [3 0 R] /Count 1 /Type /Pages >> endobj 6 0 obj << /Pages 4 0 R /Type /Catalog >> endobj 7 0 obj << /Creator (jpg2pdf) /Producer (jpg2pdf) /Trapped /False /ModDate (D:20070528134713+02'00') /CreationDate (D:20070528134713+02'00') >> endobj xref 0 8 0000000000 65535 f trailer << /Info 7 0 R /Root 6 0 R /Size 8 /ID [<62a07446f9d6fda650d12201a75fa046> <62a07446f9d6fda650d12201a75fa046>] >> startxref 534941 %%EOF ENDFOOTER # Compile (compress) pdftk "$outfile"u output "$outfile" compress rm -f "$outfile"u