Click here to Skip to main content
15,499,155 members
Articles / Programming Languages / C
Posted 3 Oct 2008


48 bookmarked

Simplest PDF Generating API for JPEG Image Content

Rate me:
Please Sign up or sign in to vote.
4.43/5 (19 votes)
19 Dec 2008CPOL1 min read
An article on how to generate PDF file based on JPEG file as page content


I was working on a project in which I need to wrap a JPEG file into PDF format. The program needs to be done in C, and after searched on the Internet, I could not find anything that I can refer to. Most of the Open Source PDF engine is based on either Java or PHP, and a few C PDF engines are huge and will add a lot of unnecessary code to my project. I decided to write this simple JPEG to PDF wrapper. And it's the result of reverse-engineering of the simplest PDF file that contains one single JPEG file. I just want to share this API so you can grab and use it if you have a similar requirement.

Using the Code

Just give an example to demonstrate how to generate a 2 page PDF file based on 2 JPEG files. Please refer to testMain.c for details. But the idea is:

int pdfByteSize, pdfOutByteSize;
unsigned char *pdfBuf;

pPDF = Jpeg2PDF_BeginDocument(8.5, 11);    
	/* pdfW, pdfH: Page Size in Inch ( 1 inch=25.4 mm ); Letter Size 8.5x11 */

if(NULL != pPDF) {
    Loop For All JPEG Files {
        ... Prepare the current JPEG File to be inserted.
        /* You'll need to know the dimension of the JPEG Image, 
	and the ByteSize of the JPEG Image */
    /* Call this after all of the JPEG image has been inserted. 
	The return value is the PDF file Byte Size */
    pdfByteSize = Jpeg2PDF_EndDocument(pPDF);
    /* Allocate the buffer for PDF Output */
    pdfBuf = malloc(pdfByteSize);
    /* Output the PDF to the pdfBuf */
   Jpeg2PDF_GetFinalDocumentAndCleanup(pPDF, pdfBuf, &pdfOutByteSize);
   ... Do something you want to the PDF file in the memory.

There are several places that you can fine-tune in the Jpeg2PDF.h file:

#define MAX_PDF_PAGES        256     /* Currently only supports less than 256 Images */

#define PDF_TOP_MARGIN        (0.0 * PDF_DOT_PER_INCH)    /* Currently No Top Margin */
#define PDF_LEFT_MARGIN        (0.0 * PDF_DOT_PER_INCH)   /* Currently No Left Margin */

That's it, guys. Enjoy.


  • Updated [2008-12-19] - Added some extra code to auto scan the current folder and automatically obtain the JPEG image dimension from the JPEG file instead of using hard coded value before.
    The JPEG image dimension code is borrowed from here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionBug or NOOB misunderstanding? Pin
John Smith27-Sep-15 23:43
MemberJohn Smith27-Sep-15 23:43 
AnswerRe: Bug or NOOB misunderstanding? Pin
Hao Hu28-Sep-15 1:05
MemberHao Hu28-Sep-15 1:05 
GeneralRe: Bug or NOOB misunderstanding? Pin
John Smith28-Sep-15 1:22
MemberJohn Smith28-Sep-15 1:22 
GeneralHow to display image in original size Pin
Denny Mathew18-Dec-14 2:26
MemberDenny Mathew18-Dec-14 2:26 
Questionwhy you only supports less than 256 Images? Pin
veeta23-Nov-14 21:20
Memberveeta23-Nov-14 21:20 
QuestionExcellent Pin
T800G27-Sep-14 11:57
MemberT800G27-Sep-14 11:57 
Questionpatches done: keep images's aspect ratio & automatic page rotate for best fit Pin
dab1826-Aug-11 7:55
Memberdab1826-Aug-11 7:55 
QuestionRemove the max 250 pages limitation? Pin
monday200015-Mar-11 2:54
Membermonday200015-Mar-11 2:54 
AnswerRe: Remove the max 250 pages limitation? Pin
Hao Hu15-Mar-11 8:15
MemberHao Hu15-Mar-11 8:15 
QuestionHow to get DPI from JPEG? Pin
monday200011-Mar-11 6:22
Membermonday200011-Mar-11 6:22 
AnswerRe: How to get DPI from JPEG? Pin
Hao Hu11-Mar-11 6:39
MemberHao Hu11-Mar-11 6:39 
GeneralRe: How to get DPI from JPEG? Pin
monday200011-Mar-11 11:09
Membermonday200011-Mar-11 11:09 
Thanks. I already found the simple solution - on[^]

Here's how I modified your function:

//Gets the JPEG size from the array of data passed to the function, file reference:
static int get_jpeg_size(unsigned char* data, unsigned int data_size, unsigned short *width, unsigned short *height)
//Check for valid JPEG image
int i=0; // Keeps track of the position within the file
if(data[i] == 0xFF && data[i+1] == 0xD8 && data[i+2] == 0xFF && data[i+3] == 0xE0)
i += 4;
// Check for valid JPEG header (null terminated JFIF)
if(data[i+2] == 'J' && data[i+3] == 'F' && data[i+4] == 'I' && data[i+5] == 'F' && data[i+6] == 0x00)
unsigned int dpi_mode = (int)data[i+9];
// 1 == 'DPI'; //Dots Per Inch
// 2 == 'DPC'; //Dots Per Cm.

unsigned short HorzRes = data[i+10] * 256 + data[i+11]; // Y-DPI, since I consider only "Y-DPI == X-DPI", this is enough.

//Retrieve the block length of the first block since the first block will not contain the size of file
unsigned short block_length = data[i] * 256 + data[i+1];

printf("dpi_mode = %d\n", dpi_mode);

printf("HorzRes = %d\n", HorzRes); // this is it - the DPI I was looking for.


I need the JPG DPI because I want to change every PDF page size according to JPG DPI.
General Pin
Hao Hu26-Aug-10 13:27
MemberHao Hu26-Aug-10 13:27 
GeneralRe: Pin
monday200012-Mar-11 22:20
Membermonday200012-Mar-11 22:20 
QuestionWhat about the XMP manipulation? [modified] Pin
monday200018-Aug-10 0:11
Membermonday200018-Aug-10 0:11 
AnswerRe: What about the XMP manipulation? Pin
Hao Hu18-Aug-10 7:09
MemberHao Hu18-Aug-10 7:09 
GeneralDesired enhancements Pin
monday200014-Aug-10 1:09
Membermonday200014-Aug-10 1:09 
GeneralRe: Desired enhancements Pin
Hao Hu14-Aug-10 16:12
MemberHao Hu14-Aug-10 16:12 
GeneralRe: Desired enhancements Pin
monday200015-Aug-10 20:44
Membermonday200015-Aug-10 20:44 
GeneralI made my program based on this one Pin
monday200013-Aug-10 2:12
Membermonday200013-Aug-10 2:12 
GeneralRe: I made my program based on this one Pin
monday200016-Mar-11 23:03
Membermonday200016-Mar-11 23:03 
GeneralRe: I made my program based on this one Pin
Hao Hu18-Mar-11 21:16
MemberHao Hu18-Mar-11 21:16 
GeneralBug found Pin
monday200013-Aug-10 2:08
Membermonday200013-Aug-10 2:08 
GeneralRe: Bug found [modified] Pin
Hao Hu13-Aug-10 6:43
MemberHao Hu13-Aug-10 6:43 
QuestionMay I re-use your code under the "GPL 2 and later" license? Pin
monday20002-Jul-10 2:32
Membermonday20002-Jul-10 2:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.