diff options
author | Albert Astals Cid <aacid@kde.org> | 2019-02-10 10:00:32 +0100 |
---|---|---|
committer | Albert Astals Cid <tsdgeos@yahoo.es> | 2019-02-28 16:10:48 +0000 |
commit | 4402e335d6a907c3eb73708a6cd50061625d431f (patch) | |
tree | 488a20d149b79e48a613ab5356c797f6cfd28046 /utils | |
parent | f4136a6353162db249f63ddb0f20611622ab61b4 (diff) |
Add new util: pdfattach
Diffstat (limited to 'utils')
-rw-r--r-- | utils/CMakeLists.txt | 9 | ||||
-rw-r--r-- | utils/pdfattach.1 | 60 | ||||
-rw-r--r-- | utils/pdfattach.cc | 112 |
3 files changed, 181 insertions, 0 deletions
diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 3516479e..3a1e0d1a 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -57,6 +57,15 @@ target_link_libraries(pdfdetach ${common_libs}) install(TARGETS pdfdetach DESTINATION bin) install(FILES pdfdetach.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1) +# pdfdetach +set(pdfattach_SOURCES ${common_srcs} + pdfattach.cc +) +add_executable(pdfattach ${pdfattach_SOURCES}) +target_link_libraries(pdfattach ${common_libs}) +install(TARGETS pdfattach DESTINATION bin) +install(FILES pdfattach.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1) + # pdffonts set(pdffonts_SOURCES ${common_srcs} pdffonts.cc diff --git a/utils/pdfattach.1 b/utils/pdfattach.1 new file mode 100644 index 00000000..c9589a78 --- /dev/null +++ b/utils/pdfattach.1 @@ -0,0 +1,60 @@ +.\" Copyright 2019 Albert Astals Cid +.TH pdfattach 1 "10 Febuary 2019" +.SH NAME +pdfattach \- Portable Document Format (PDF) document embedded file +creator (version 3.03) +.SH SYNOPSIS +.B pdfattach +[options] +.I input-PDF-file file-to-attach output-PDF-file +.SH DESCRIPTION +.B Pdfattach +adds a new embedded file (attachment) to an existing Portable +Document Format (PDF) file. +.SH OPTIONS +.TP +.B \-replace +Replace embedded file with same name (if it exists) +.TP +.B \-v +Print copyright and version information. +.TP +.B \-h +Print usage information. +.RB ( \-help +and +.B \-\-help +are equivalent.) +.SH EXIT CODES +.TP +0 +No error. +.TP +1 +Error opening input PDF file. +.TP +2 +Error opening file to attach. +.TP +3 +Output file already exists. +.TP +3 +There is already an attached file with that name. +.TP +5 +Error saving the output file. +.SH AUTHOR +The pdfattach software and documentation are copyright 2019 The Poppler developers +.SH "SEE ALSO" +.BR pdfdetach (1), +.BR pdfimages (1), +.BR pdfinfo (1), +.BR pdftocairo (1), +.BR pdftohtml (1), +.BR pdftoppm (1), +.BR pdftops (1), +.BR pdftotext (1) +.BR pdfseparate (1), +.BR pdfsig (1), +.BR pdfunite (1) diff --git a/utils/pdfattach.cc b/utils/pdfattach.cc new file mode 100644 index 00000000..e4f40a32 --- /dev/null +++ b/utils/pdfattach.cc @@ -0,0 +1,112 @@ +//======================================================================== +// +// pdfattach.cc +// +// This file is licensed under the GPLv2 or later +// +// Copyright (C) 2019 Albert Astals Cid <aacid@kde.org> +// +// To see a description of the changes please see the Changelog file that +// came with your tarball or type make ChangeLog if you are building from git +// +//======================================================================== + +#include "config.h" +#include <poppler-config.h> +#include "gbasename.h" +#include "parseargs.h" +#include "GlobalParams.h" +#include "PDFDoc.h" +#include "PDFDocFactory.h" +#include "Error.h" +#include "ErrorCodes.h" +#include "Win32Console.h" + +static bool doReplace = false; +static bool printVersion = false; +static bool printHelp = false; + +static ArgDesc argDesc[] = { + {"-replace", argFlag, &doReplace, 0, + "replace embedded file with same name (if it exists)"}, + {"-v", argFlag, &printVersion, 0, + "print copyright and version info"}, + {"-h", argFlag, &printHelp, 0, + "print usage information"}, + {"-help", argFlag, &printHelp, 0, + "print usage information"}, + {"--help", argFlag, &printHelp, 0, + "print usage information"}, + {"-?", argFlag, &printHelp, 0, + "print usage information"}, + { } +}; + +static bool fileExists(const char *filePath) +{ + FILE *f = openFile(filePath, "r"); + if (f != nullptr) { + fclose(f); + return true; + } + return false; +} + +int main(int argc, char *argv[]) { + Win32Console win32Console(&argc, &argv); + + // parse args + const bool ok = parseArgs(argDesc, &argc, argv); + if (!ok || argc != 4 || printVersion || printHelp) { + fprintf(stderr, "pdfattach version %s\n", PACKAGE_VERSION); + fprintf(stderr, "%s\n", popplerCopyright); + fprintf(stderr, "%s\n", xpdfCopyright); + if (!printVersion) { + printUsage("pdfattach", "<input-PDF-file> <file-to-attach> <output-PDF-file>", argDesc); + } + return 99; + } + const GooString pdfFileName(argv[1]); + const GooString attachFilePath(argv[2]); + + // init GlobalParams + auto gp = std::make_unique<GlobalParams>(); + globalParams = gp.get(); + + // open PDF file + std::unique_ptr<PDFDoc> doc(PDFDocFactory().createPDFDoc(pdfFileName, nullptr, nullptr)); + + if (!doc->isOk()) { + fprintf(stderr, "Couldn't open %s\n", pdfFileName.c_str()); + return 1; + } + + std::unique_ptr<GooFile> attachFile(GooFile::open(&attachFilePath)); + if (!attachFile) { + fprintf(stderr, "Couldn't open %s\n", attachFilePath.c_str()); + return 2; + } + + if (fileExists(argv[3])) { + fprintf(stderr, "File %s already exists.\n", argv[3]); + return 3; + } + + const std::string attachFileName = gbasename(attachFilePath.c_str()); + + if (!doReplace && doc->getCatalog()->hasEmbeddedFile(attachFileName)) { + fprintf(stderr, "There is already an embedded file named %s.\n", attachFileName.c_str()); + return 4; + } + + doc->getCatalog()->addEmbeddedFile(attachFile.get(), attachFileName); + + const GooString outputPdfFilePath(argv[3]); + const int saveResult = doc->saveAs(&outputPdfFilePath); + if (saveResult != errNone) { + fprintf(stderr, "Couldn't save the file properly.\n"); + return 5; + } + + return 0; +} |