 Chromium Code Reviews
 Chromium Code Reviews Issue 1840793002:
  Starting to add some documentation.  (Closed) 
  Base URL: https://pdfium.googlesource.com/pdfium.git@master
    
  
    Issue 1840793002:
  Starting to add some documentation.  (Closed) 
  Base URL: https://pdfium.googlesource.com/pdfium.git@master| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 # Getting Started with PDFium | |
| 2 | |
| 3 [TOC] | |
| 4 | |
| 5 This guide walks through some examples of using the PDFium library. For an | |
| 6 example of using PDFium see the [Chromium PDF Plugin][chrome-plugin]. | |
| 7 | |
| 8 ## Prerequisites | |
| 9 | |
| 10 You will need the PDFium library on your computer. You can see the | |
| 11 [README](/README.md) for instructions on getting and installing PDFium. | |
| 12 | |
| 13 *** note | |
| 14 You must compile PDFium without both V8 and XFA support for the examples | |
| 15 here to work. V8 can be compiled out by providing | |
| 16 `GYP_DEFINES="pdf_enable_v8=0 pdf_enable_xfa=0" build/gyp_pdfium`. | |
| 17 | |
| 18 See the [V8 Getting Started][pdfium-v8] guide for how to | |
| 19 initialize PDFium when V8 is compiled into the binary. | |
| 20 *** | |
| 21 | |
| 22 ## PDFium Headers | |
| 23 | |
| 24 PDFium's API has been broken up over several headers. You only need to include | |
| 25 the headers for functionality you use in your application. The full set of | |
| 26 headers can be found in the [public/ folder of the repository][pdfium-public]. | |
| 27 | |
| 28 In all cases you'll need to include `fpdfview.h` as it defines the needed | |
| 29 methods for initialization and destruction of the library. | |
| 30 | |
| 31 ## Initializing PDFium | |
| 32 | |
| 33 The first step to using PDFium is to initialize the library. Having done so, | |
| 34 you'll need to destroy the library when you're finished. When initializing the | |
| 35 library you provide the `FPDF_LIBRARY_CONFIG` parameters to | |
| 36 `FPDF_InitLibraryWithConfig`. | |
| 37 | |
| 38 ```c | |
| 39 #include <fpdfview.h> | |
| 40 | |
| 41 int main() { | |
| 42 FPDF_LIBRARY_CONFIG config; | |
| 43 config.version = 2; | |
| 44 config.m_pUserFontPaths = NULL; | |
| 45 config.m_pIsolate = NULL; | |
| 46 config.m_v8EmbedderSlot = 0; | |
| 47 | |
| 48 FPDF_InitLibraryWithConfig(&config); | |
| 49 | |
| 50 FPDF_DestroyLibrary(); | |
| 51 return 0; | |
| 52 } | |
| 53 ``` | |
| 54 | |
| 55 Currently the `config.version` must be set to `2`. `m_pUserFontPaths` can be | |
| 56 used to override the font paths searched by PDFium. If you wish to use your | |
| 57 own font paths pass a `NULL` terminated list of `const char*` paths to use. | |
| 58 | |
| 59 `m_pIsolate` and `m_v8EmbedderSlot` are both used to configure the V8 | |
| 60 javascript engine. In the first case, you can provide an isolate through | |
| 61 `m_pIsolate` for PDFium to use to store per-isolate data. Passing `NULL` will | |
| 62 case PDFium to allocate a new isolate. `m_v8EmbedderSlot` is the embedder data | |
| 63 slot to use in the v8::Isolate to store PDFium data. The value must be between | |
| 64 0 and v8::Internals::kNumIsolateDataSlots. Typically, 0 is a good choice. | |
| 65 | |
| 66 For more information on using Javascript see the [V8 Getting Started][pdfium-v8] | |
| 67 guide. | |
| 68 | |
| 69 *** aside | |
| 70 PDFium is built as a set of static libraries. You'll need to specify them all on | |
| 71 the link line in order to compile. My build line was: | |
| 72 | |
| 73 ``` | |
| 74 PDF_LIBS="-lpdfium -lfpdfapi -lfxge -lfpdfdoc -lfxcrt -lfx_agg \ | |
| 75 -lfxcodec -lfx_lpng -lfx_libopenjpeg -lfx_lcms2 -lfx_freetype -ljpeg \ | |
| 76 -lfx_zlib -lfdrm -lpdfwindow -lbigint -lformfiller -ljavascript \ | |
| 77 -lfxedit" | |
| 78 PDF_DIR=<path/to/pdfium> | |
| 79 | |
| 80 clang -I $PDF_DIR/public -o init init.c -L $PDF_DIR/out/Debug -lstdc++ -framewor k AppKit $PDF_LIBS | |
| 81 ``` | |
| 82 | |
| 83 The `-framework AppKit` as needed as I'm building on a Mac. Internally PDFium | |
| 84 uses C++, which is why `-lstdc++` is required on the link line. | |
| 85 *** | |
| 86 | |
| 87 ## Loading a Document | |
| 
dsinclair
2016/03/29 01:55:29
Added the start of a loading section.
 | |
| 88 | |
| 89 One of the main objects in PDFium is the `FPDF_DOCUMENT`. The object will allow | |
| 90 access to information from PDFs. There are four ways to to create a | |
| 91 `FPDF_DOCUMENT`. `FPDF_CreateNewDocument` will create an empty object which | |
| 92 can be used to create PDFs. For more information see the | |
| 93 [PDF Editing Guide][pdfium-edit-guide]. | |
| 94 | |
| 95 Loading an existing document is done in one of three ways, loading from file, | |
| 
Tom Sepez
2016/04/01 00:06:19
three ways:
 
dsinclair
2016/04/01 13:08:52
Done.
 | |
| 96 loading from memory and a custom loader. In all three cases you'll provide a | |
| 
Tom Sepez
2016/04/01 00:06:19
memory, or loading via a custom loader.
 
dsinclair
2016/04/01 13:08:52
Done.
 | |
| 97 `FPDF_BYTESTRING` which is the password needed to unlock the PDF, if encrypted. | |
| 98 If the file is not encrypted the password maybe `NULL`. | |
| 
Tom Sepez
2016/04/01 00:06:19
can be
 
dsinclair
2016/04/01 13:08:52
Done.
 | |
| 99 | |
| 100 The two simplest methods are loading from disk and from memory. To load from | |
| 
Tom Sepez
2016/04/01 00:06:19
"loading from file and loading from memory" 
we s
 
dsinclair
2016/04/01 13:08:52
Done.
 | |
| 101 disk, provide the file to open, including extension. For loading from memory | |
| 
Tom Sepez
2016/04/01 00:06:19
file, you'll provide the name of the file
 
dsinclair
2016/04/01 13:08:52
Done.
 | |
| 102 provide the data buffer containing the PDF and the length. | |
| 
Tom Sepez
2016/04/01 00:06:19
you'll provide a data buffer ... and its length.
 
dsinclair
2016/04/01 13:08:52
Done.
 | |
| 103 | |
| 104 ```c | |
| 105 FPDF_STRING test_doc = "test_doc.pdf"; | |
| 106 FPDF_DOCUMENT doc = FPDF_LoadDocument(test_doc, NULL); | |
| 107 if (!doc) { | |
| 108 return 1; | |
| 109 } | |
| 110 | |
| 111 FPDF_CloseDocument(doc); | |
| 112 | |
| 113 ``` | |
| 114 | |
| 115 In all three cases, `FPDF_LoadDocument`, `FPDF_LoadMemDocument`, | |
| 116 `FPDF_LoadCustomDocument` a return of `NULL` indicates an error opening the | |
| 117 document or the file was not found. | |
| 
Tom Sepez
2016/04/01 00:06:20
or that the file
 
dsinclair
2016/04/01 13:08:52
Done.
 | |
| 118 | |
| 119 You can use `FPDF_GetLastError` to determine what went wrong. | |
| 120 | |
| 121 ```c | |
| 122 #include <fpdfview.h> | |
| 123 #include <unistd.h> | |
| 124 #include <stdio.h> | |
| 125 | |
| 126 int main() { | |
| 127 FPDF_LIBRARY_CONFIG config; | |
| 128 config.version = 2; | |
| 129 config.m_pUserFontPaths = NULL; | |
| 130 config.m_pIsolate = NULL; | |
| 131 config.m_v8EmbedderSlot = 0; | |
| 132 | |
| 133 FPDF_InitLibraryWithConfig(&config); | |
| 134 | |
| 135 FPDF_DOCUMENT doc = FPDF_LoadDocument(test_doc, NULL); | |
| 136 if (!doc) { | |
| 137 unsigned long err = FPDF_GetLastError(); | |
| 138 fprintf(stderr, "Load pdf docs unsuccessful: "); | |
| 139 switch (err) { | |
| 140 case FPDF_ERR_SUCCESS: | |
| 141 fprintf(stderr, "Success"); | |
| 142 break; | |
| 143 case FPDF_ERR_UNKNOWN: | |
| 144 fprintf(stderr, "Unknown error"); | |
| 145 break; | |
| 146 case FPDF_ERR_FILE: | |
| 147 fprintf(stderr, "File not found or could not be opened"); | |
| 148 break; | |
| 149 case FPDF_ERR_FORMAT: | |
| 150 fprintf(stderr, "File not in PDF format or corrupted"); | |
| 151 break; | |
| 152 case FPDF_ERR_PASSWORD: | |
| 153 fprintf(stderr, "Password required or incorrect password"); | |
| 154 break; | |
| 155 case FPDF_ERR_SECURITY: | |
| 156 fprintf(stderr, "Unsupported security scheme"); | |
| 157 break; | |
| 158 case FPDF_ERR_PAGE: | |
| 159 fprintf(stderr, "Page not found or content error"); | |
| 160 break; | |
| 161 default: | |
| 162 fprintf(stderr, "Unknown error %ld", err); | |
| 163 } | |
| 164 fprintf(stderr, ".\n"); | |
| 165 goto EXIT; | |
| 166 } | |
| 167 | |
| 168 FPDF_CloseDocument(doc); | |
| 169 EXIT: | |
| 170 FPDF_DestroyLibrary(); | |
| 171 return 0; | |
| 172 ``` | |
| 173 | |
| 174 The more interesting open is custom loading. Using a custom loader it's possible | |
| 
Tom Sepez
2016/04/01 00:06:19
While the above are simple, the preferable techniq
 
dsinclair
2016/04/01 13:08:52
Done.
 | |
| 175 to load the pieces of the document as needed. This is useful for loading | |
| 176 documents over the network. | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 [chrome-plugin]: https://chromium.googlesource.com/chromium/src/+/master/pdf/ | |
| 182 [pdfium-public]: https://pdfium.googlesource.com/pdfium/+/master/public/ | |
| 183 [pdfium-v8]: /docs/v8-getting-started.md | |
| 184 [pdfium-edit-guide]: /docs/pdfium-edit-guide.md | |
| OLD | NEW |