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