| 1 | # PoDoFo [](https://github.com/podofo/podofo/actions/workflows/build-linux.yml) [](https://github.com/podofo/podofo/actions/workflows/build-mac.yml) [](https://github.com/podofo/podofo/actions/workflows/build-win.yml) |
| 2 | |
| 3 | 1. [What is PoDoFo?](#what-is-podofo) |
| 4 | 2. [Requirements](#requirements) |
| 5 | 3. [Licensing](#licensing) |
| 6 | 4. [Development quickstart](#development-quickstart) |
| 7 | 5. [String encoding and buffer conventions](#string-encoding-and-buffer-conventions) |
| 8 | 6. [API Stability](#api-stability) |
| 9 | 7. [PoDoFo tools](#podofo-tools) |
| 10 | 8. [TODO](#todo) |
| 11 | 9. [FAQ](#faq) |
| 12 | 10. [No warranty](#no-warranty) |
| 13 | 11. [Contributions](#contributions) |
| 14 | 12. [Authors](#authors) |
| 15 | |
| 16 | ## What is PoDoFo? |
| 17 | |
| 18 | PoDoFo is a s a free portable C++ library to work with the PDF file format. |
| 19 | |
| 20 | PoDoFo provides classes to parse a PDF file and modify its content |
| 21 | into memory. The changes can be written back to disk easily. |
| 22 | Besides PDF parsing PoDoFo also provides facilities to create your |
| 23 | own PDF files from scratch. It currently does not |
| 24 | support rendering PDF content. |
| 25 | |
| 26 | ## Requirements |
| 27 | |
| 28 | To build PoDoFo lib you'll need a c++17 compiler, |
| 29 | CMake 3.16 and the following libraries: |
| 30 | |
| 31 | * freetype2 |
| 32 | * fontconfig (required for Unix platforms, optional for Windows) |
| 33 | * OpenSSL (1.1 and 3.0 are supported) |
| 34 | * LibXml2 |
| 35 | * zlib |
| 36 | * libjpeg (optional) |
| 37 | * libtiff (optional) |
| 38 | * libpng (optional) |
| 39 | * libidn (optional) |
| 40 | |
| 41 | For the most polular toolchains, PoDoFo requires the following |
| 42 | minimum versions: |
| 43 | |
| 44 | * msvc++ 14.16 (VS 2017 15.9) |
| 45 | * gcc 8.1 |
| 46 | * clang/llvm 7.0 |
| 47 | |
| 48 | It is regularly tested with the following IDE/toolchains versions: |
| 49 | |
| 50 | * Visual Studio 2017 15.9 |
| 51 | * Visual Studio 2019 16.11 |
| 52 | * Visual Studio 2022 17.3 |
| 53 | * gcc 9.3.1 |
| 54 | * XCode 13.3 |
| 55 | * NDK r23b |
| 56 | |
| 57 | GCC 8.1 support [broke](https://github.com/podofo/podofo/issues/116), but it could be reinstanced. |
| 58 | |
| 59 | ## Licensing |
| 60 | |
| 61 | PoDoFo library is licensed under the [LGPL 2.0](https://spdx.org/licenses/LGPL-2.0-or-later.html) or later terms. |
| 62 | PoDoFo tools are licensed under the [GPL 2.0](https://spdx.org/licenses/GPL-2.0-or-later.html) or later terms. |
| 63 | |
| 64 | ## Development quickstart |
| 65 | |
| 66 | PoDoFo is known to compile through a multitude of package managers (including `apt-get`, [brew](https://brew.sh/), [vcpkg](https://vcpkg.io/), [Conan](https://conan.io/)), and has public continous integration working in [Ubuntu Linux](https://github.com/podofo/podofo/blob/master/.github/workflows/build-linux.yml), [MacOS](https://github.com/podofo/podofo/blob/master/.github/workflows/build-linux.yml) and |
| 67 | [Windows](https://github.com/podofo/podofo/blob/master/.github/workflows/build-win.yml), bootstrapping the CMake project, building and testing the library. It's highly recommended to build PoDoFo using such package managers. |
| 68 | |
| 69 | There's also a playground area in the repository where you can have |
| 70 | access to pre-build dependencies for some popular architectures/operating systems: |
| 71 | the playground is the recommended setting to develop the library and reproduce bugs, |
| 72 | while it's not recommended for the deployment of your application using PoDoFo. |
| 73 | Have a look to the [Readme](https://github.com/podofo/podofo/tree/master/playground) there. |
| 74 | |
| 75 | > **Warning**: PoDoFo is known to be working in cross-compilation toolchains (eg. Android/iOS development), but support may not provided in such scenarios. If you decide to manually build dependencies you are assumed to know how to identity possible library clashes/mismatches and how to deal with compilation/linking problems that can arise in your system. |
| 76 | |
| 77 | ### Build with apt-get |
| 78 | |
| 79 | From the source root run: |
| 80 | |
| 81 | ``` |
| 82 | sudo apt-get install -y libfontconfig1-dev libfreetype-dev libxml2-dev libssl-dev libjpeg-dev libpng-dev libtiff-dev libidn11-dev |
| 83 | mkdir build |
| 84 | cd build |
| 85 | cmake -DCMAKE_BUILD_TYPE=Debug .. |
| 86 | cmake --build . --config Debug |
| 87 | ``` |
| 88 | |
| 89 | ### Build with brew |
| 90 | |
| 91 | Install [brew](https://brew.sh/), then from the source root run: |
| 92 | |
| 93 | ``` |
| 94 | brew install fontconfig freetype openssl libxml2 jpeg-turbo libpng libtiff libidn |
| 95 | mkdir build |
| 96 | cd build |
| 97 | cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_FIND_FRAMEWORK=NEVER -DCMAKE_PREFIX_PATH=`brew --prefix` -DFontconfig_INCLUDE_DIR=`brew --prefix fontconfig`/include -DOPENSSL_ROOT_DIR=`brew --prefix openssl@3` .. |
| 98 | cmake --build . --config Debug |
| 99 | ``` |
| 100 | |
| 101 | ### Build with Conan |
| 102 | |
| 103 | Install [conan](https://docs.conan.io/1/installation.html), then from source root run: |
| 104 | |
| 105 | ``` |
| 106 | mkdir build |
| 107 | cd build |
| 108 | conan install .. |
| 109 | cmake -DCMAKE_BUILD_TYPE=Debug .. |
| 110 | cmake --build . --config Debug |
| 111 | ``` |
| 112 | |
| 113 | ### Build with vcpkg |
| 114 | |
| 115 | Follow the vcpkg [quickstart](https://vcpkg.io/en/getting-started.html) guide to setup the package manager repository first. |
| 116 | In Windows, it may be also useful to set the enviroment variable `VCPKG_DEFAULT_TRIPLET` to `x64-windows` to default installing 64 bit dependencies |
| 117 | and define a `VCPKG_INSTALLATION_ROOT` variable with the location of the repository as created in the quickstart. |
| 118 | |
| 119 | Then from source root run: |
| 120 | |
| 121 | ``` |
| 122 | vcpkg install fontconfig freetype libxml2 openssl libjpeg-turbo libpng tiff zlib |
| 123 | mkdir build |
| 124 | cd build |
| 125 | cmake -DCMAKE_TOOLCHAIN_FILE=C:\vcpkg\scripts\buildsystems\vcpkg.cmake -DCMAKE_BUILD_TYPE=Debug .. |
| 126 | cmake --build . --config Debug |
| 127 | ``` |
| 128 | |
| 129 | ### CMake switches |
| 130 | |
| 131 | - `PODOFO_BUILD_TEST`: Build the unit tests, defaults to TRUE; |
| 132 | |
| 133 | - `PODOFO_BUILD_EXAMPLES`: Build the examples, defaults to TRUE; |
| 134 | |
| 135 | - `PODOFO_BUILD_TOOLS`: Build the PoDoFo tools, defaults to FALSE. See |
| 136 | the relevant [section](https://github.com/podofo/podofo/#podofo-tools) in the Readme; |
| 137 | |
| 138 | - `PODOFO_BUILD_LIB_ONLY`: If TRUE, it will build only the library component. |
| 139 | This unconditionally disable building tests, examples and tools; |
| 140 | |
| 141 | - `PODOFO_BUILD_STATIC`: If TRUE, build the library as a static object and use it in tests, |
| 142 | examples and tools. By default a shared library is built. |
| 143 | |
| 144 | ## String encoding and buffer conventions |
| 145 | |
| 146 | All `std::strings` or `std::string_view` in the library are intended |
| 147 | to hold UTF-8 encoded string content. `PdfString` and `PdfName` constructors |
| 148 | accept UTF-8 encoded strings by default (`PdfName` accept only characters in the |
| 149 | `PdfDocEncoding` char set, though). `charbuff` abd `bufferview` |
| 150 | instead represent a generic octet buffer. |
| 151 | |
| 152 | ## API migration |
| 153 | |
| 154 | PoDoFo has an unstable API that is the result of an extensive API review of PoDoFo 0.9.x. At this [link](https://github.com/podofo/podofo/wiki/PoDoFo-API-migration-guide/#098---0100) you can find an incomplete guide on migrating 0.9.8 code to 0.10.0. It is expected PoDoFo will converge to a stable API as soon as the review process is completed. See [API Stability](https://github.com/podofo/podofo/wiki/API-Stability) for more details. |
| 155 | |
| 156 | ## PoDoFo Tools |
| 157 | |
| 158 | > **Warning**: Tools are currently **untested** and **unmaintained**. |
| 159 | |
| 160 | PoDoFo tools are still available in the source [tree](https://github.com/podofo/podofo/) |
| 161 | but their compilation is disabled by default because they are unsted/unmaintained, |
| 162 | and will not receive support until their status is cleared. It's not recommended to include them in software distributions. |
| 163 | If you want to build them make sure to bootstrap the CMake project with ```-DPODOFO_BUILD_TOOLS=TRUE```. |
| 164 | Tools are conveniently enabled in the [playground](https://github.com/podofo/podofo/tree/master/playground) |
| 165 | at least to ensure library changes won't break their compilation. |
| 166 | |
| 167 | ## TODO |
| 168 | |
| 169 | There's a [TODO](https://github.com/podofo/podofo/blob/master/TODO.md) list, or look |
| 170 | at the [issue](https://github.com/podofo/podofo/issues) tracker. |
| 171 | |
| 172 | ## FAQ |
| 173 | |
| 174 | **Q: How do I sign a document?** |
| 175 | |
| 176 | **A:** The signing procedure is still as low level as it was before the |
| 177 | ongoing source changes. This is going to change, soon!, with a new high-level |
| 178 | API for signing being worked on, which will be fully unit tested. For now you |
| 179 | should check the `podofosign` tool (**WARNING**: untested) which should give |
| 180 | you the idea how to sign documents creating a *CMS* structure directly with |
| 181 | OpenSSL. |
| 182 | To describe the procedure briefly, one has to fully Implement a `PdfSigner`, |
| 183 | retrieve or create a `PdfSignature` field, create an output device (see next question) |
| 184 | and use `PoDoFo::SignDocument(doc, device, signer, signature)`. When signing, |
| 185 | the sequence of calls of `PdfSignature` works in this way: method `PdfSigner::Reset()` |
| 186 | is called first, then the `PdfSigner::ComputeSignature(buffer, dryrun)` is called with |
| 187 | an empty buffer and the `dryrun` argument set to `true`. In this call one can just |
| 188 | resize the buffer overestimating the required size for the signature, or just |
| 189 | compute a fake signature that must be saved on the buffer. Then a sequence of |
| 190 | `PdfSigner::AppendData(buffer)` are called, receiving all the document data to |
| 191 | be signed. A final `PdfSigner::ComputeSignature(buffer, dryrun)` is called, with |
| 192 | the `dryrun` parameter set to `false`. The buffer on this call is cleared (capacity |
| 193 | is not altered) or not accordingly to the value of `PdfSigner::SkipBufferClear()`. |
| 194 | |
| 195 | |
| 196 | **Q: `PdfMemDocument::SaveUpdate()` or `PoDoFo::SignDocument()` write only a partial |
| 197 | file: why so and why there's no mechanism to seamlessly handle the incremental |
| 198 | update as it was in PoDoFo? What should be done to correctly update/sign the |
| 199 | document?** |
| 200 | |
| 201 | **A:** The previous mechanism in PoDoFo required enablement of document for |
| 202 | incremental updates, which is a decision step which I believe should be not |
| 203 | be necessary. Also: |
| 204 | 1. In case of file loaded document it still required to perform the update in |
| 205 | the same file, and the check was performed on the path of the files being |
| 206 | operated to, which is unsafe; |
| 207 | 2. In case of buffers worked for one update/signing operation but didn't work |
| 208 | for following operations. |
| 209 | |
| 210 | The idea is to implement a more explicit mechanism that makes more clear |
| 211 | and/or enforces the fact that the incremental update must be performed on the |
| 212 | same file in case of file loaded documents or that underlying buffer will grow |
| 213 | following subsequent operations in case of buffer loaded documents. |
| 214 | Before that, as a workaround, a couple of examples showing the correct operation |
| 215 | to either update or sign a document (file or buffer loaded) are presented. |
| 216 | Save an update on a file loaded document, by copying the source to another |
| 217 | destination: |
| 218 | |
| 219 | ``` |
| 220 | string inputPath; |
| 221 | string outputPath; |
| 222 | auto input = std::make_shared<FileStreamDevice>(inputPath); |
| 223 | FileStreamDevice output(outputPath, FileMode::Create); |
| 224 | input->CopyTo(output); |
| 225 | |
| 226 | PdfMemDocument doc; |
| 227 | doc.LoadFromDevice(input); |
| 228 | |
| 229 | doc.SaveUpdate(output); |
| 230 | ``` |
| 231 | |
| 232 | Sign a buffer loaded document: |
| 233 | |
| 234 | ``` |
| 235 | bufferview inputBuffer; |
| 236 | charbuff outputBuffer; |
| 237 | auto input = std::make_shared<SpanStreamDevice>(inputBuffer); |
| 238 | BufferStreamDevice output(outputBuffer); |
| 239 | input->CopyTo(output); |
| 240 | |
| 241 | PdfMemDocument doc; |
| 242 | doc.LoadFromDevice(input); |
| 243 | // Retrieve/create the signature, create the signer, ... |
| 244 | PoDoFo::SignDocument(doc, output, signer, signature); |
| 245 | ``` |
| 246 | |
| 247 | **Q: Can I sign a document a second time?** |
| 248 | |
| 249 | **A:** Yes, this is tested, but to make sure this will work you'll to re-parse the document a second time, |
| 250 | as re-using the already loaded document is still untested (this may change later). For example do as it follows: |
| 251 | |
| 252 | ``` |
| 253 | bufferview inputBuffer; |
| 254 | charbuff outputBuffer; |
| 255 | auto input = std::make_shared<SpanStreamDevice>(inputBuffer); |
| 256 | BufferStreamDevice output(outputBuffer); |
| 257 | input->CopyTo(output); |
| 258 | |
| 259 | { |
| 260 | PdfMemDocument doc; |
| 261 | doc.LoadFromDevice(input); |
| 262 | // Retrieve/create the signature, create the signer, ... |
| 263 | PoDoFo::SignDocument(doc, output, signer, signature); |
| 264 | } |
| 265 | |
| 266 | input = std::make_shared<SpanStreamDevice>(output); |
| 267 | { |
| 268 | PdfMemDocument doc; |
| 269 | doc.LoadFromDevice(input); |
| 270 | // Retrieve/create the signature, create the signer, ... |
| 271 | PoDoFo::SignDocument(doc, output, signer, signature); |
| 272 | } |
| 273 | ``` |
| 274 | |
| 275 | ## No warranty |
| 276 | |
| 277 | PoDoFo may or may not work for your needs and comes with absolutely no |
| 278 | warranty. Serious bugs, including security flaws, may be fixed at arbitrary |
| 279 | timeframes, or not fixed at all. Priority of implementing new features |
| 280 | and bug fixing are decided according to the interests and personal |
| 281 | preferences of the maintainers. If you need PoDoFo to integrate a feature |
| 282 | or bug fix that is critical to your workflow, the most welcome and fastest |
| 283 | approach is to [contribute](https://github.com/podofo/podofo/edit/master/README.md#contributions) |
| 284 | high-quality patches. |
| 285 | |
| 286 | ## Contributions |
| 287 | |
| 288 | Please subscribe to the project mailing [list](https://sourceforge.net/projects/podofo/lists/podofo-users) |
| 289 | which is still followed by several of the original developers of PoDoFo. |
| 290 | A gitter [community](https://gitter.im/podofo/community) has also been created to ease some more informal chatter. |
| 291 | If you find a bug and know how to fix it, or you want to add a small feature, you're welcome to send a [pull request](https://github.com/podofo/podofo/pulls), |
| 292 | providing it follows the [coding style](https://github.com/podofo/podofo/blob/master/CODING-STYLE.md) |
| 293 | of the project. As a minimum requisite, any contribution should be: |
| 294 | * valuable for a multitude of people and not only self relevant for the contributor; |
| 295 | * consistent with surrounding code and not result in unpredictable behavior and/or bugs. |
| 296 | |
| 297 | Other reasons for the rejection, or hold, of a pull request may be: |
| 298 | |
| 299 | * the proposed code is incomplete or hacky; |
| 300 | * the change doesn't fit the scope of PoDoFo; |
| 301 | * the change shows lack of knowledge/mastery of the PDF specification and/or C++ language; |
| 302 | * the change breaks automatic tests performed by the maintainer; |
| 303 | * general lack of time in reviewing and merging the change. |
| 304 | |
| 305 | If you need to implement a bigger feature or refactor, ask first if |
| 306 | it was already planned. The feature may be up for grabs, meaning that it's open for external contributions. |
| 307 | Please write in the relevant issue that you started to work on that, to receive some feedback/coordination. |
| 308 | If it's not, it means that the refactor/feature is planned to be implemented later by the maintainer(s). |
| 309 | If the feature is not listed in the issues, add it and/or create a [discussion](https://github.com/podofo/podofo/discussions) |
| 310 | to receive some feedback and discuss some basic design choices. |
| 311 | |
| 312 | ## Authors |
| 313 | |
| 314 | > **Warning**: Please don't use personal email addresses for technical support inquries, but create |
| 315 | github [issues](https://github.com/podofo/podofo/issues) instead. |
| 316 | |
| 317 | PoDoFo is currently developed and maintained by |
| 318 | [Francesco Pretto](mailto:[email protected]), together with Dominik Seichter and others. See the file |
| 319 | [AUTHORS.md](https://github.com/podofo/podofo/blob/master/AUTHORS.md) for more details. |
| 320 |