Chapter 17
Creating New PDFs

cpdf -create-pdf [-create-pdf-pages <n>]
     [-create-pdf-papersize <paper size>] -o out.pdf

cpdf -typeset <text file> [-create-pdf-papersize <size>]
     [-font <font>] [-font-size <size>] -o out.pdf

17.1 A new blank PDF

We can build a new PDF file, given a number of pages and a paper size. The default is one page, A4 portrait.

cpdf -create-pdf -create-pdf-pages 20
        -create-pdf-papersize usletterportrait -o out.pdf

The standard paper sizes are listed in Section 3.1, or you may specify the width and height directly, as described in the same chapter.

17.2 Convert a text file to PDF

A basic text to PDF convertor is included in cpdf. It takes a UTF8 text file (ASCII is a subset of UTF8) and typesets it ragged-right, splitting on whitespace. Both Windows and Unix line endings are allowed.

cpdf -typeset file.txt -create-pdf-papersize a3portrait
        -font Courier -font-size 10 -o out.pdf

The standard paper sizes are listed in Section 3.1, or you may specify the width and height directly, as described in the same chapter. The standard fonts are listed in chapter 8. The default font is TimesRoman and the default size is 12.

Java Interface

 
/* CHAPTER 17. Creating New PDFs */ 
 
/** Creates a blank document with pages of the given width (in points), 
height (in points), and number of pages. 
@param w width of page 
@param h height of page 
@param pages number of pages */ 
public native Pdf blankDocument(double w, double h, int pages) 
    throws CpdfError; 
 
/** Makes a blank document given a page size and number of pages. 
@param papersize paper size, such as {@link #a0portrait a0portrait} 
@param pages number of pages */ 
public native Pdf blankDocumentPaper(int papersize, int pages) 
    throws CpdfError; 
 
/** Typesets a UTF8 text file ragged right on a page of size w * h in 
points in the given font and font size. 
@param w width of page 
@param h height of page 
@param font font, such as {@link #timesRoman timesRoman} 
@param fontsize font size 
@param filename file name */ 
public Pdf textToPDF(double w, double h, int font, double fontsize, 
                     String filename) 
    throws CpdfError; 
 
/** Typesets a UTF8 text file ragged right on a page of the given size in 
the given font and font size. 
@param papersize paper size, such as {@link #a0portrait a0portrait} 
@param font font, such as {@link #timesRoman timesRoman} 
@param fontsize font size 
@param filename file name */ 
public Pdf textToPDFPaper(int papersize, int font, double fontsize, 
                          String filename) 
    throws CpdfError;