| 1 | /** |
| 2 | * SPDX-FileCopyrightText: (C) 2005 Dominik Seichter <[email protected]> |
| 3 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | */ |
| 5 | |
| 6 | #include <podofo/private/PdfDeclarationsPrivate.h> |
| 7 | #include <podofo/podofo.h> |
| 8 | |
| 9 | #include <cstdlib> |
| 10 | #include <cstdio> |
| 11 | |
| 12 | // Conversation constant to convert 1/1000th mm to 1/72 inch |
| 13 | #define CONVERSION_CONSTANT 0.002834645669291339 |
| 14 | |
| 15 | using namespace std; |
| 16 | using namespace PoDoFo; |
| 17 | |
| 18 | #define BORDER_TOP 10000 * CONVERSION_CONSTANT |
| 19 | #define BORDER_LEFT 10000 * CONVERSION_CONSTANT |
| 20 | #define FONT_SIZE 12.0 |
| 21 | #define DEFAULT_FONT "Arial" |
| 22 | |
| 23 | void print_help() |
| 24 | { |
| 25 | printf("Usage: podofotxt2pdf [inputfile] [outputfile]\n\n"); |
| 26 | printf("Optional parameters:\n"); |
| 27 | printf("\t-fontname [name]\t Use the font [name]\n"); |
| 28 | printf("\nPoDoFo Version: %s\n\n", PODOFO_VERSION_STRING); |
| 29 | } |
| 30 | |
| 31 | void draw(const char* buffer, PdfDocument& document, const string_view& fontName) |
| 32 | { |
| 33 | PdfPainter painter; |
| 34 | Rect size; |
| 35 | |
| 36 | double x = BORDER_LEFT; |
| 37 | double y = BORDER_TOP; |
| 38 | const char* cursor = buffer; |
| 39 | |
| 40 | size = PdfPage::CreateStandardPageSize(PdfPageSize::A4); |
| 41 | auto font = document.GetFonts().SearchFont(fontName); |
| 42 | auto page = &document.GetPages().CreatePage(size); |
| 43 | y = size.Height - y; |
| 44 | if (font == nullptr) |
| 45 | PODOFO_RAISE_ERROR(PdfErrorCode::InvalidHandle); |
| 46 | |
| 47 | painter.SetCanvas(*page); |
| 48 | painter.TextState.SetFont(*font, FONT_SIZE); |
| 49 | |
| 50 | while (*buffer) |
| 51 | { |
| 52 | if (*buffer == '\n') |
| 53 | { |
| 54 | painter.DrawText(cursor, x, y); |
| 55 | cursor = ++buffer; |
| 56 | y -= font->GetMetrics().GetLineSpacing(); |
| 57 | if (y < BORDER_TOP) |
| 58 | { |
| 59 | page = &document.GetPages().CreatePage(size); |
| 60 | painter.SetCanvas(*page); |
| 61 | y = size.Height - y; |
| 62 | } |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | buffer++; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | painter.FinishDrawing(); |
| 71 | } |
| 72 | |
| 73 | void init(const string_view& inputPath, const string_view& outputPath, const string_view& fontName) |
| 74 | { |
| 75 | FILE* file; |
| 76 | |
| 77 | PdfStreamedDocument doc(outputPath); |
| 78 | |
| 79 | char* buffer; |
| 80 | long size; |
| 81 | |
| 82 | file = fopen(inputPath.data(), "rb"); // read it as binary if we are going to compare sizes! |
| 83 | if (!file) |
| 84 | { |
| 85 | PODOFO_RAISE_ERROR(PdfErrorCode::InvalidHandle); |
| 86 | } |
| 87 | |
| 88 | if (fseek(file, 0x00, SEEK_END) == -1) |
| 89 | { |
| 90 | fclose(file); |
| 91 | PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidDeviceOperation, "Failed to seek to the end of the file"); |
| 92 | } |
| 93 | |
| 94 | size = ftell(file); |
| 95 | if (size == -1) |
| 96 | { |
| 97 | fclose(file); |
| 98 | PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidDeviceOperation, "Failed to read size of the file"); |
| 99 | } |
| 100 | |
| 101 | buffer = static_cast<char*>(malloc(sizeof(char) * (size + 1))); |
| 102 | fseek(file, 0x00, SEEK_SET); |
| 103 | if (!buffer) |
| 104 | { |
| 105 | fclose(file); |
| 106 | PODOFO_RAISE_ERROR(PdfErrorCode::OutOfMemory); |
| 107 | } |
| 108 | |
| 109 | // read the whole file into memory at once. |
| 110 | // this not very efficient, but as this is |
| 111 | // a library demonstration I do not care. |
| 112 | // If anyone wants to improve this: Go for it! |
| 113 | if (static_cast<long>(fread(buffer, sizeof(char), size, file)) != size) |
| 114 | { |
| 115 | free(buffer); |
| 116 | fclose(file); |
| 117 | PODOFO_RAISE_ERROR(PdfErrorCode::UnexpectedEOF); |
| 118 | } |
| 119 | |
| 120 | fclose(file); |
| 121 | |
| 122 | buffer[size] = '\0'; |
| 123 | |
| 124 | draw(buffer, doc, fontName); |
| 125 | |
| 126 | doc.GetMetadata().SetCreator(PdfString("podofotxt2pdf")); |
| 127 | doc.GetMetadata().SetTitle(PdfString("Converted to PDF from a text file")); |
| 128 | doc.Close(); |
| 129 | |
| 130 | free(buffer); |
| 131 | } |
| 132 | |
| 133 | void Main(const cspan<string_view>& args) |
| 134 | { |
| 135 | if (args.size() < 3) |
| 136 | { |
| 137 | print_help(); |
| 138 | exit(-1); |
| 139 | } |
| 140 | |
| 141 | string_view inputPath; |
| 142 | string_view outputPath; |
| 143 | string_view fontName = DEFAULT_FONT; |
| 144 | |
| 145 | for (unsigned i = 1; i < args.size(); i++) |
| 146 | { |
| 147 | |
| 148 | if ("-fontname" == args[i]) |
| 149 | { |
| 150 | fontName = args[++i]; |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | if (inputPath == NULL) |
| 155 | inputPath = args[i]; |
| 156 | else |
| 157 | outputPath = args[i]; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | init(inputPath, outputPath, fontName); |
| 162 | } |