| 1 | /** |
| 2 | * Copyright (C) 2006 by Dominik Seichter <[email protected]> |
| 3 | * |
| 4 | * Licensed under GNU General Public License 2.0 or later. |
| 5 | * Some rights reserved. See COPYING, AUTHORS. |
| 6 | */ |
| 7 | |
| 8 | // Include the standard headers for cout to write |
| 9 | // some output to the console. |
| 10 | #include <iostream> |
| 11 | |
| 12 | // Now include all PoDoFo header files, to have access |
| 13 | // to all functions of PoDoFo and so that you do not have |
| 14 | // to care about the order of includes. |
| 15 | // |
| 16 | // You should always use podofo.h and not try to include |
| 17 | // the required headers on your own. |
| 18 | #include <podofo/podofo.h> |
| 19 | |
| 20 | // All PoDoFo classes are member of the PoDoFo namespace. |
| 21 | using namespace std; |
| 22 | using namespace PoDoFo; |
| 23 | |
| 24 | void PrintHelp() |
| 25 | { |
| 26 | cout << "This is a example application for the PoDoFo PDF library." << endl |
| 27 | << "It creates a small PDF file containing the text >Hello World!<" << endl |
| 28 | << "Please see https://github.com/podofo/podofo for more information" << endl << endl; |
| 29 | cout << "Usage:" << endl; |
| 30 | cout << " helloworld [outputfile.pdf]" << endl << endl; |
| 31 | } |
| 32 | |
| 33 | void HelloWorld(const string_view& filename) |
| 34 | { |
| 35 | // The document is written directly to filename while being created. |
| 36 | PdfMemDocument document; |
| 37 | |
| 38 | // PdfPainter is the class which is able to draw text and graphics |
| 39 | // directly on a PdfPage object. |
| 40 | PdfPainter painter; |
| 41 | |
| 42 | // A PdfFont object is required to draw text on a PdfPage using a PdfPainter. |
| 43 | // PoDoFo will find the font using fontconfig on your system and embedd truetype |
| 44 | // fonts automatically in the PDF file. |
| 45 | PdfFont* font; |
| 46 | |
| 47 | try |
| 48 | { |
| 49 | // The PdfDocument object can be used to create new PdfPage objects. |
| 50 | // The PdfPage object is owned by the PdfDocument will also be deleted automatically |
| 51 | // by the PdfDocument object. |
| 52 | // |
| 53 | // You have to pass only one argument, i.e. the page size of the page to create. |
| 54 | // There are predefined enums for some common page sizes. |
| 55 | auto& page = document.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4)); |
| 56 | |
| 57 | // Set the page as drawing target for the PdfPainter. |
| 58 | // Before the painter can draw, a page has to be set first. |
| 59 | painter.SetCanvas(page); |
| 60 | |
| 61 | // Create a PdfFont object using the font "Arial". |
| 62 | // The font is found on the system using fontconfig and embedded into the |
| 63 | // PDF file. If Arial is not available, a default font will be used. |
| 64 | // |
| 65 | // The created PdfFont will be deleted by the PdfDocument. |
| 66 | font = document.GetFonts().SearchFont("Arial"); |
| 67 | |
| 68 | // If the PdfFont object cannot be allocated return an error. |
| 69 | if (font == nullptr) |
| 70 | throw runtime_error("Invalid handle"); |
| 71 | |
| 72 | auto& metrics = font->GetMetrics(); |
| 73 | cout << "The font name is "<< metrics.GetFontName() << endl; |
| 74 | cout << "The family font name is " << metrics.GetFontFamilyName() << endl; |
| 75 | cout << "The font file path is " << metrics.GetFilePath() << endl; |
| 76 | cout << "The font face index is " << metrics.GetFaceIndex() << endl; |
| 77 | |
| 78 | // Set the font as default font for drawing. |
| 79 | // A font has to be set before you can draw text on |
| 80 | // a PdfPainter. |
| 81 | painter.TextState.SetFont(*font, 18); |
| 82 | |
| 83 | // You could set a different color than black to draw |
| 84 | // the text. |
| 85 | // |
| 86 | // painter.SetColor(1.0, 0.0, 0.0); |
| 87 | |
| 88 | // Actually draw the line "Hello World!" on to the PdfPage at |
| 89 | // the position 2cm,2cm from the top left corner. |
| 90 | // Please remember that PDF files have their origin at the |
| 91 | // bottom left corner. Therefore we substract the y coordinate |
| 92 | // from the page height. |
| 93 | // |
| 94 | // The position specifies the start of the baseline of the text. |
| 95 | // |
| 96 | // All coordinates in PoDoFo are in PDF units. |
| 97 | painter.DrawText("ABCDEFGHIKLMNOPQRSTVXYZ", 56.69, page.GetRect().Height - 56.69); |
| 98 | |
| 99 | try |
| 100 | { |
| 101 | // Add also some non-ASCII characters (Cyrillic alphabet) |
| 102 | painter.DrawText("АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЫЭЮЯ", 56.69, page.GetRect().Height - 80); |
| 103 | } |
| 104 | catch (PdfError& err) |
| 105 | { |
| 106 | if (err.GetCode() == PdfErrorCode::InvalidFontData) |
| 107 | cout << "WARNING: The matched font \"" << metrics.GetFontName() << "\" doesn't support cyrillic" << endl; |
| 108 | } |
| 109 | |
| 110 | // Tell PoDoFo that the page has been drawn completely. |
| 111 | // This required to optimize drawing operations inside in PoDoFo |
| 112 | // and has to be done whenever you are done with drawing a page. |
| 113 | painter.FinishDrawing(); |
| 114 | |
| 115 | // Set some additional information on the PDF file. |
| 116 | document.GetMetadata().SetCreator(PdfString("examplahelloworld - A PoDoFo test application")); |
| 117 | document.GetMetadata().SetAuthor(PdfString("Dominik Seichter")); |
| 118 | document.GetMetadata().SetTitle(PdfString("Hello World")); |
| 119 | document.GetMetadata().SetSubject(PdfString("Testing the PoDoFo PDF Library")); |
| 120 | document.GetMetadata().SetKeywords(vector<string>({ "Test", "PDF", "Hello World" })); |
| 121 | |
| 122 | // The last step is to close the document. |
| 123 | document.Save(filename); |
| 124 | } |
| 125 | catch (PdfError& e) |
| 126 | { |
| 127 | // All PoDoFo methods may throw exceptions |
| 128 | // make sure that painter.FinishPage() is called |
| 129 | // or who will get an assert in its destructor |
| 130 | try |
| 131 | { |
| 132 | painter.FinishDrawing(); |
| 133 | } |
| 134 | catch (...) |
| 135 | { |
| 136 | // Ignore errors this time |
| 137 | } |
| 138 | |
| 139 | throw e; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | int main(int argc, char* argv[]) |
| 144 | { |
| 145 | // Check if a filename was passed as commandline argument. |
| 146 | // If more than 1 argument or no argument is passed, |
| 147 | // a help message is displayed and the example application |
| 148 | // will quit. |
| 149 | if (argc != 2) |
| 150 | { |
| 151 | PrintHelp(); |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | // All PoDoFo functions will throw an exception in case of an error. |
| 156 | // |
| 157 | // You should catch the exception to either fix it or report |
| 158 | // back to the user. |
| 159 | // |
| 160 | // All exceptions PoDoFo throws are objects of the class PdfError. |
| 161 | // Thats why we simply catch PdfError objects. |
| 162 | try |
| 163 | { |
| 164 | // Call the drawing routing which will create a PDF file |
| 165 | // with the filename of the output file as argument. |
| 166 | HelloWorld(argv[1]); |
| 167 | } |
| 168 | catch (PdfError& err) |
| 169 | { |
| 170 | // We have to check if an error has occurred. |
| 171 | // If yes, we return and print an error message |
| 172 | // to the commandline. |
| 173 | err.PrintErrorMsg(); |
| 174 | return (int)err.GetCode(); |
| 175 | } |
| 176 | |
| 177 | // The PDF was created sucessfully. |
| 178 | cout << endl |
| 179 | << "Created a PDF file containing the line \"Hello World!\": " << argv[1] << endl << endl; |
| 180 | |
| 181 | return 0; |
| 182 | } |