| 1 | /** |
| 2 | * SPDX-FileCopyrightText: (C) 2010 Ian Ashley <[email protected]> |
| 3 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | */ |
| 5 | |
| 6 | #include <podofo/private/PdfDeclarationsPrivate.h> |
| 7 | |
| 8 | #include <iostream> |
| 9 | #include <iterator> |
| 10 | #include <string> |
| 11 | #include <cstdlib> |
| 12 | #include <cstdio> |
| 13 | |
| 14 | #ifdef _MSC_VER |
| 15 | #include <io.h> |
| 16 | #include <fcntl.h> |
| 17 | #endif |
| 18 | |
| 19 | #include <podofo/podofo.h> |
| 20 | |
| 21 | using namespace std; |
| 22 | using namespace PoDoFo; |
| 23 | |
| 24 | void Main(const cspan<string_view>& args) |
| 25 | { |
| 26 | PdfMemDocument* doc = nullptr; |
| 27 | PdfCommon::SetMaxLoggingSeverity(PdfLogSeverity::None); |
| 28 | if (args.size() != 2 && args.size() != 4) |
| 29 | { |
| 30 | cout << "Syntax" << endl; |
| 31 | cout << " " << args[0] << " <pdf file> - display the XMP in a file (use \"-\" to specify stdin)" << endl; |
| 32 | cout << "or" << endl; |
| 33 | cout << " " << args[0] << " <src pdf file> <xmp file> <new pdf file> - create a new PDF with the XMP in" << endl; |
| 34 | exit(-1); |
| 35 | } |
| 36 | |
| 37 | if (args[1] == "-") |
| 38 | { |
| 39 | cin >> noskipws; |
| 40 | #ifdef _MSC_VER |
| 41 | _setmode(_fileno(stdin), _O_BINARY); // @TODO: MSVC specific binary setmode -- not sure if other platforms need it |
| 42 | cin.sync_with_stdio(); |
| 43 | #endif |
| 44 | istream_iterator<char> it(cin); |
| 45 | istream_iterator<char> end; |
| 46 | string buffer(it, end); |
| 47 | doc = new PdfMemDocument(); |
| 48 | doc->LoadFromBuffer(buffer); |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | doc = new PdfMemDocument(); |
| 53 | doc->Load(args[1]); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | if (args.size() == 2) |
| 58 | { |
| 59 | auto metadata = doc->GetCatalog().GetMetadataObject(); |
| 60 | if (metadata == nullptr) |
| 61 | { |
| 62 | cout << "No metadata" << endl; |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | auto stream = metadata->GetStream(); |
| 67 | if (stream != nullptr) |
| 68 | { |
| 69 | charbuff buffer = stream->GetCopy(); |
| 70 | printf("%s", buffer.data()); |
| 71 | printf("\n"); |
| 72 | fflush(stdout); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (args.size() == 4) |
| 78 | { |
| 79 | char* xmpBuf; |
| 80 | FILE* fp; |
| 81 | |
| 82 | if ((fp = fopen(args[2].data(), "rb")) == nullptr) |
| 83 | cout << "Cannot open " << args[2] << endl; |
| 84 | else |
| 85 | { |
| 86 | if (fseek(fp, 0, SEEK_END) == -1) |
| 87 | { |
| 88 | fclose(fp); |
| 89 | PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidDeviceOperation, "Failed to seek to the end of the file"); |
| 90 | } |
| 91 | |
| 92 | long rc = ftell(fp); |
| 93 | if (rc == -1) |
| 94 | { |
| 95 | fclose(fp); |
| 96 | PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidDeviceOperation, "Failed to read size of the file"); |
| 97 | } |
| 98 | |
| 99 | size_t xmpLen = (size_t)rc; |
| 100 | xmpBuf = new char[xmpLen]; |
| 101 | if (!xmpBuf) |
| 102 | { |
| 103 | fclose(fp); |
| 104 | PODOFO_RAISE_ERROR(PdfErrorCode::OutOfMemory); |
| 105 | } |
| 106 | |
| 107 | if (fseek(fp, 0, SEEK_SET) == -1) |
| 108 | { |
| 109 | delete[] xmpBuf; |
| 110 | fclose(fp); |
| 111 | |
| 112 | PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidDeviceOperation, "Failed to seek to the beginning of the file"); |
| 113 | } |
| 114 | |
| 115 | if (fread(xmpBuf, 1, xmpLen, fp) != xmpLen) |
| 116 | { |
| 117 | delete[] xmpBuf; |
| 118 | fclose(fp); |
| 119 | |
| 120 | PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidDeviceOperation, "Failed to read whole file into the memory"); |
| 121 | } |
| 122 | |
| 123 | auto metadata = doc->GetCatalog().GetMetadataObject(); |
| 124 | if (metadata != nullptr) |
| 125 | { |
| 126 | metadata->GetOrCreateStream().SetData({ xmpBuf, xmpLen }); |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | metadata = &doc->GetObjects().CreateDictionaryObject("Metadata"); |
| 131 | metadata->GetDictionary().AddKey("Subtype", PdfName("XML")); |
| 132 | metadata->GetOrCreateStream().SetData({ xmpBuf, xmpLen }); |
| 133 | doc->GetCatalog().GetDictionary().AddKey("Metadata", metadata->GetIndirectReference()); |
| 134 | } |
| 135 | |
| 136 | delete[] xmpBuf; |
| 137 | doc->Save(args[3]); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (doc) |
| 142 | delete doc; |
| 143 | } |