home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 February / PCWorld_2002-02_cd.bin / Software / Vyzkuste / pdflib / pdflib-4.0.1.sit / pdflib-4.0.1 / clients / text2pdf.c < prev   
Encoding:
C/C++ Source or Header  |  2001-07-04  |  6.9 KB  |  251 lines  |  [TEXT/CWIE]

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