Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C
Article

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 92K   3K   48   45
An article on how to generate PDF file based on JPEG file as page content

Introduction

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:

C++
PJPEG2PDF pPDF;
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 */
        Jpeg2PDF_AddJpeg(pPDF, JPEG_IMGW, JPEG_IMGH, JPEG_BYTE_SIZE, 
		JPEG_DATA_POINTER, IS_COLOR_JPEG);
    } 
    
    /* 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:

C++
#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.

History

  • 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.

License

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 22:43
John Smith27-Sep-15 22:43 
AnswerRe: Bug or NOOB misunderstanding? Pin
Hao Hu28-Sep-15 0:05
Hao Hu28-Sep-15 0:05 
GeneralRe: Bug or NOOB misunderstanding? Pin
John Smith28-Sep-15 0:22
John Smith28-Sep-15 0:22 
GeneralHow to display image in original size Pin
Denny Mathew18-Dec-14 1:26
Denny Mathew18-Dec-14 1:26 
Questionwhy you only supports less than 256 Images? Pin
veeta23-Nov-14 20:20
veeta23-Nov-14 20:20 
QuestionExcellent Pin
T800G27-Sep-14 10:57
T800G27-Sep-14 10:57 
Questionpatches done: keep images's aspect ratio & automatic page rotate for best fit Pin
dab1826-Aug-11 6:55
dab1826-Aug-11 6:55 
QuestionRemove the max 250 pages limitation? Pin
monday200015-Mar-11 1:54
monday200015-Mar-11 1:54 
AnswerRe: Remove the max 250 pages limitation? Pin
Hao Hu15-Mar-11 7:15
Hao Hu15-Mar-11 7:15 
QuestionHow to get DPI from JPEG? Pin
monday200011-Mar-11 5:22
monday200011-Mar-11 5:22 
AnswerRe: How to get DPI from JPEG? Pin
Hao Hu11-Mar-11 5:39
Hao Hu11-Mar-11 5:39 
GeneralRe: How to get DPI from JPEG? Pin
monday200011-Mar-11 10:09
monday200011-Mar-11 10:09 
Generalhttps://sourceforge.net/projects/jpeg2pdf/ Pin
Hao Hu26-Aug-10 12:27
Hao Hu26-Aug-10 12:27 
GeneralRe: https://sourceforge.net/projects/jpeg2pdf/ Pin
monday200012-Mar-11 21:20
monday200012-Mar-11 21:20 
QuestionWhat about the XMP manipulation? [modified] Pin
monday200017-Aug-10 23:11
monday200017-Aug-10 23:11 
Hello, Hao Hu.

It would be nice also if your program could manipulate the XMP-metadata inside PDF files.

XMP is contaned just inside another PDF-object - using XML syntax.

XMP manipulation is a useful thing because XMP provides the different metadata information about a PDF document like Author, Title, Keywords, etc.

The XMP specification is available from Adobe:

http://www.adobe.com/devnet/xmp/

Here is an example of XMP inside PDF:

380 0 obj<</Subtype/XML/Length 3247/Type/Metadata>>stream
<?xpacket begin="?»?" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="3.1-701">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about=""
xmlns:xap="http://ns.adobe.com/xap/1.0/">
<xap:CreateDate>2009-11-03T17:06:02Z</xap:CreateDate>
<xap:ModifyDate>2009-11-03T20:08:04+03:00</xap:ModifyDate>
<xap:MetadataDate>2009-11-03T20:08:04+03:00</xap:MetadataDate>
</rdf:Description>
<rdf:Description rdf:about=""
xmlns:pdf="http://ns.adobe.com/pdf/1.3/">
<pdf:Producer>ABBYY FineReader 9.0 Professional Edition</pdf:Producer>
</rdf:Description>
<rdf:Description rdf:about=""
xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:format>application/pdf</dc:format>
</rdf:Description>
<rdf:Description rdf:about=""
xmlns:xapMM="http://ns.adobe.com/xap/1.0/mm/">
<xapMM:DocumentID>uuid:6c762139-bdde-4faa-9adc-b1e164c6db73</xapMM:DocumentID>
<xapMM:InstanceID>uuid:a2270d76-823e-405c-b377-b3a7f72c4575</xapMM:InstanceID>
</rdf:Description>
</rdf:RDF>
</x:xmpmeta>





















<?xpacket end="w"?>
endstream
endobj



-- Modified Wednesday, August 18, 2010 5:17 AM
AnswerRe: What about the XMP manipulation? Pin
Hao Hu18-Aug-10 6:09
Hao Hu18-Aug-10 6:09 
GeneralDesired enhancements Pin
monday200014-Aug-10 0:09
monday200014-Aug-10 0:09 
GeneralRe: Desired enhancements Pin
Hao Hu14-Aug-10 15:12
Hao Hu14-Aug-10 15:12 
GeneralRe: Desired enhancements Pin
monday200015-Aug-10 19:44
monday200015-Aug-10 19:44 
GeneralI made my program based on this one Pin
monday200013-Aug-10 1:12
monday200013-Aug-10 1:12 
GeneralRe: I made my program based on this one Pin
monday200016-Mar-11 22:03
monday200016-Mar-11 22:03 
GeneralRe: I made my program based on this one Pin
Hao Hu18-Mar-11 20:16
Hao Hu18-Mar-11 20:16 
GeneralBug found Pin
monday200013-Aug-10 1:08
monday200013-Aug-10 1:08 
GeneralRe: Bug found [modified] Pin
Hao Hu13-Aug-10 5:43
Hao Hu13-Aug-10 5:43 
QuestionMay I re-use your code under the "GPL 2 and later" license? Pin
monday20002-Jul-10 1:32
monday20002-Jul-10 1: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.