1/*---------------------------------------------------------------------------* 2 | PDFlib - A library for generating PDF on the fly | 3 +---------------------------------------------------------------------------+ 4 | Copyright (c) 1997-2004 Thomas Merz and PDFlib GmbH. All rights reserved. | 5 +---------------------------------------------------------------------------+ 6 | | 7 | This software is subject to the PDFlib license. It is NOT in the | 8 | public domain. Extended versions and commercial licenses are | 9 | available, please check http://www.pdflib.com. | 10 | | 11 *---------------------------------------------------------------------------*/ 12 13/* $Id: text2pdf.c 14574 2005-10-29 16:27:43Z bonefish $ 14 * 15 * Convert text files to PDF 16 * 17 */ 18 19#include <stdio.h> 20#include <string.h> 21#include <stdlib.h> 22 23#if defined(__CYGWIN32__) 24#include <getopt.h> 25#elif defined(WIN32) 26int getopt(int argc, char * const argv[], const char *optstring); 27extern char *optarg; 28extern int optind; 29#elif !defined(WIN32) && !defined(MAC) 30#include <unistd.h> 31#endif 32 33 34#ifdef WIN32 35#include <process.h> 36#endif 37 38#ifdef NeXT 39#include <libc.h> /* for getopt(), optind, optarg */ 40#endif 41 42#ifdef __CYGWIN32__ 43#include <getopt.h> /* for getopt(), optind, optarg */ 44#endif 45 46#if defined WIN32 || defined __DJGPP__ || \ 47 defined __OS2__ || defined __IBMC__ || defined __IBMCPP__ || \ 48 defined __POWERPC__ || defined __CFM68K__ || defined __MC68K__ || \ 49 defined AS400 || defined __ILEC400__ 50 51#define READMODE "rb" 52 53#else 54 55#define READMODE "r" 56 57#endif /* Mac, Windows, and OS/2 platforms */ 58 59/* figure out whether or not we're running on an EBCDIC-based machine */ 60#define ASCII_A 0x41 61#define PLATFORM_A 'A' 62#define EBCDIC_BRACKET 0x4A 63#define PLATFORM_BRACKET '[' 64 65#if (ASCII_A != PLATFORM_A && EBCDIC_BRACKET == PLATFORM_BRACKET) 66#define PDFLIB_EBCDIC 67#endif 68 69#include "pdflib.h" 70 71static void 72usage(void) 73{ 74 fprintf(stderr, "text2pdf - convert text files to PDF.\n"); 75 fprintf(stderr, "(C) PDFlib GmbH and Thomas Merz 1997-2004\n"); 76 fprintf(stderr, "usage: text2pdf [options] [textfile]\n"); 77 fprintf(stderr, "Available options:\n"); 78 fprintf(stderr, 79 "-e encoding font encoding to use. Common encoding names:\n"); 80 fprintf(stderr, 81 " winansi, macroman, ebcdic, or user-defined\n"); 82 fprintf(stderr, " host = default encoding of this platform\n"); 83 fprintf(stderr, "-f fontname name of font to use\n"); 84 fprintf(stderr, "-h height page height in points\n"); 85 fprintf(stderr, "-m margin margin size in points\n"); 86 fprintf(stderr, "-o filename PDF output file name\n"); 87 fprintf(stderr, "-s size font size\n"); 88 fprintf(stderr, "-w width page width in points\n"); 89 90 exit(1); 91} 92 93#define BUFLEN 512 94 95int 96main(int argc, char *argv[]) 97{ 98 char buf[BUFLEN], *s; 99 char *pdffilename = NULL; 100 FILE *textfile = stdin; 101 PDF *p; 102 int opt; 103 int font; 104 char *fontname, *encoding; 105 float fontsize; 106 float x, y, width = a4_width, height = a4_height, margin = 20; 107 char ff, nl; 108 109 fontname = "Courier"; 110 fontsize = 12.0; 111 encoding = "host"; 112 nl = '\n'; 113 ff = '\f'; 114 115 while ((opt = getopt(argc, argv, "e:f:h:m:o:s:w:")) != -1) 116 switch (opt) { 117 case 'e': 118 encoding = optarg; 119 break; 120 121 case 'f': 122 fontname = optarg; 123 break; 124 125 case 'h': 126 height = atoi(optarg); 127 if (height < 0) { 128 fprintf(stderr, "Error: bad page height %f!\n", height); 129 usage(); 130 } 131 break; 132 133 case 'm': 134 margin = atoi(optarg); 135 if (margin < 0) { 136 fprintf(stderr, "Error: bad margin %f!\n", margin); 137 usage(); 138 } 139 break; 140 141 case 'o': 142 pdffilename = optarg; 143 break; 144 145 case 's': 146 fontsize = atoi(optarg); 147 if (fontsize < 0) { 148 fprintf(stderr, "Error: bad font size %f!\n", fontsize); 149 usage(); 150 } 151 break; 152 153 case 'w': 154 width = atoi(optarg); 155 if (width < 0) { 156 fprintf(stderr, "Error: bad page width %f!\n", width); 157 usage(); 158 } 159 break; 160 161 case '?': 162 default: 163 usage(); 164 } 165 166 if (!strcmp(encoding, "ebcdic")) { 167 /* form feed is 0x0C in both ASCII and EBCDIC */ 168 nl = 0x15; 169 } 170 171 if (pdffilename == NULL) 172 usage(); 173 174 if (optind < argc) { 175 if ((textfile = fopen(argv[optind], READMODE)) == NULL) { 176 fprintf(stderr, "Error: cannot open input file %s.\n",argv[optind]); 177 exit(2); 178 } 179 } else 180 textfile = stdin; 181 182 p = PDF_new(); 183 if (p == NULL) { 184 fprintf(stderr, "Error: cannot open output file %s.\n", pdffilename); 185 exit(1); 186 } 187 188 PDF_open_file(p, pdffilename); 189 190 PDF_set_info(p, "Title", "Converted text"); 191 PDF_set_info(p, "Creator", "text2pdf"); 192 193 x = margin; 194 y = height - margin; 195 196 while ((s = fgets(buf, BUFLEN, textfile)) != NULL) { 197 if (s[0] == ff) { 198 if (y == height - margin) 199 PDF_begin_page(p, width, height); 200 PDF_end_page(p); 201 y = height - margin; 202 continue; 203 } 204 205 if (s[0] != '\0' && s[strlen(s) - 1] == nl) 206 s[strlen(s) - 1] = '\0'; /* remove newline character */ 207 208 if (y < margin) { /* page break necessary? */ 209 y = height - margin; 210 PDF_end_page(p); 211 } 212 213 if (y == height - margin) { 214 PDF_begin_page(p, width, height); 215 font = PDF_findfont(p, fontname, encoding, 0); 216 PDF_setfont(p, font, fontsize); 217 PDF_set_text_pos(p, x, y); 218 y -= fontsize; 219 } 220 221 PDF_continue_text(p, s); 222 y -= fontsize; 223 224 } 225 226 if (y != height - margin) 227 PDF_end_page(p); 228 229 PDF_close(p); 230 PDF_delete(p); 231 232 exit(0); 233} 234