Back to index

tetex-bin  3.0
pdftosrc.cc
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 1996-2002 Han The Thanh, <thanh@pdftex.org>
00003 
00004 This file is part of pdfTeX.
00005 
00006 pdfTeX is free software; you can redistribute it and/or modify
00007 it under the terms of the GNU General Public License as published by
00008 the Free Software Foundation; either version 2 of the License, or
00009 (at your option) any later version.
00010 
00011 pdfTeX is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with pdfTeX; if not, write to the Free Software
00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 $Id: //depot/Build/source.development/TeX/texk/web2c/pdftexdir/pdftosrc.cc#10 $
00021 */
00022 
00023 #include <stdlib.h>
00024 #include <stddef.h>
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <ctype.h>
00028 #include <aconf.h>
00029 #include <GString.h>
00030 #include <gmem.h>
00031 #include <gfile.h>
00032 #include "Object.h"
00033 #include "Stream.h"
00034 #include "Array.h"
00035 #include "Dict.h"
00036 #include "XRef.h"
00037 #include "Catalog.h"
00038 #include "Page.h"
00039 #include "GfxFont.h"
00040 #include "PDFDoc.h"
00041 #include "GlobalParams.h"
00042 #include "Error.h"
00043 
00044 static XRef *xref = 0;
00045 
00046 int main(int argc, char *argv[])
00047 {
00048     char *p, buf[1024];
00049     PDFDoc *doc;
00050     GString *fileName;
00051     Stream *s;
00052     Object srcStream, srcName, catalogDict;
00053     FILE *outfile;
00054     char *outname;
00055     int objnum = 0, objgen = 0;
00056     int c;
00057     fprintf(stderr, "pdftosrc version %s\n", xpdfVersion);
00058     if (argc < 2) {
00059         fprintf(stderr, "Usage: pdftosrc <PDF-file> [<stream-object-number>]\n");
00060         exit(1);
00061     }
00062     fileName = new GString(argv[1]);
00063     globalParams = new GlobalParams();
00064     doc = new PDFDoc(fileName);
00065     if (!doc->isOk()) {
00066         fprintf(stderr, "Invalid PDF file\n");
00067         exit(1);
00068     }
00069     if (argc >= 3) {
00070         objnum = atoi(argv[2]);
00071         if (argc >= 4)
00072             objgen = atoi(argv[3]);
00073     }
00074     xref = doc->getXRef();
00075     catalogDict.initNull();
00076     xref->getCatalog(&catalogDict);
00077     if (!catalogDict.isDict("Catalog")) {
00078         fprintf(stderr, "No Catalog found\n");
00079         exit(1);
00080     }
00081     srcStream.initNull();
00082     if (objnum == 0) {
00083         catalogDict.dictLookup("SourceObject", &srcStream);
00084         if (!srcStream.isStream("SourceFile")) {
00085             fprintf(stderr, "No SourceObject found\n");
00086             exit(1);
00087         }
00088         srcName.initNull();
00089         srcStream.getStream()->getDict()->lookup("SourceName", &srcName);
00090         if (!srcName.isString()) {
00091             fprintf(stderr, "No SourceName found\n");
00092             exit(1);
00093         }
00094         outname = srcName.getString()->getCString();
00095         // We cannot free srcName, as objname shares its string.
00096         // srcName.free();
00097     }
00098     else {
00099         xref->fetch(objnum, objgen, &srcStream);
00100         if (!srcStream.isStream()) {
00101             fprintf(stderr, "Not a Stream object\n");
00102             exit(1);
00103         }
00104         sprintf(buf, "%s", fileName->getCString());
00105         if ((p = strrchr(buf, '.')) == 0)
00106             p = strchr(buf, 0);
00107         if (objgen == 0)
00108             sprintf(p, ".%i", objnum);
00109         else
00110             sprintf(p, ".%i+%i", objnum, objgen);
00111         outname = buf;
00112     }
00113     if (!(outfile = fopen(outname, "wb"))) {
00114         fprintf(stderr, "Cannot open file \"%s\" for writing\n", outname);
00115         exit(1);
00116     }
00117     s = srcStream.getStream();
00118     s->reset();
00119     while ((c = s->getChar()) != EOF)
00120         fputc(c, outfile);
00121     if (objnum == 0)
00122         fprintf(stderr, "Source file extracted to %s\n", outname);
00123     else
00124         fprintf(stderr, "Stream object extracted to %s\n", outname);
00125     fclose(outfile);
00126     catalogDict.free();
00127     srcStream.free();
00128     delete doc;
00129     delete globalParams;
00130 }