xref: /aosp_15_r20/external/pdfium/samples/simple_with_v8.cc (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2020 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // No-frills example of how to initialize and call into a PDFium environment
6 // including V8 support (but not XFA) from C++. Since the V8 API is in C++,
7 // C++ is required in this case (not just C).
8 
9 #include <string.h>
10 
11 #include "public/fpdf_edit.h"
12 #include "public/fpdf_formfill.h"
13 #include "public/fpdfview.h"
14 #include "v8/include/libplatform/libplatform.h"
15 #include "v8/include/v8-array-buffer.h"
16 #include "v8/include/v8-initialization.h"
17 #include "v8/include/v8-isolate.h"
18 
main(int argc,const char * argv[])19 int main(int argc, const char* argv[]) {
20   // V8 must be initialized before the PDFium library if using V8.
21   v8::V8::InitializeICUDefaultLocation(argv[0]);
22   v8::V8::InitializeExternalStartupData(argv[0]);
23   v8::Platform* platform = v8::platform::NewDefaultPlatform().release();
24   v8::V8::InitializePlatform(platform);
25   v8::V8::Initialize();
26 
27   v8::Isolate::CreateParams params;
28   params.array_buffer_allocator = static_cast<v8::ArrayBuffer::Allocator*>(
29       FPDF_GetArrayBufferAllocatorSharedInstance());
30   v8::Isolate* isolate = v8::Isolate::New(params);
31 
32   // The PDF library must be initialized before creating a document.
33   FPDF_LIBRARY_CONFIG config;
34   memset(&config, 0, sizeof(config));
35   config.version = 3;
36   config.m_pIsolate = isolate;
37   config.m_pPlatform = platform;
38   FPDF_InitLibraryWithConfig(&config);
39 
40   // The document must be created before creating a form-fill environment.
41   // Typically use FPDF_LoadDocument() for pre-existing documents. Here, we
42   // create a new blank document for simplicity.
43   FPDF_DOCUMENT doc = FPDF_CreateNewDocument();
44 
45   IPDF_JSPLATFORM jsplatform;
46   memset(&jsplatform, 0, sizeof(jsplatform));
47 
48   FPDF_FORMFILLINFO formfillinfo;
49   memset(&formfillinfo, 0, sizeof(formfillinfo));
50   formfillinfo.version = 1;
51   formfillinfo.m_pJsPlatform = &jsplatform;
52 
53   FPDF_FORMHANDLE form_handle =
54       FPDFDOC_InitFormFillEnvironment(doc, &formfillinfo);
55 
56   // Typically use FPDF_LoadPage() for pre-existing pages. Here, we
57   // create a new blank page for simplicity.
58   FPDF_PAGE page = FPDFPage_New(doc, 0, 640.0, 480.0);
59   FORM_OnAfterLoadPage(page, form_handle);
60   FORM_DoPageAAction(page, form_handle, FPDFPAGE_AACTION_OPEN);
61 
62   // Do actual work with the page here.
63 
64   FORM_DoPageAAction(page, form_handle, FPDFPAGE_AACTION_CLOSE);
65   FORM_OnBeforeClosePage(page, form_handle);
66   FPDF_ClosePage(page);
67 
68   FORM_DoDocumentAAction(form_handle, FPDFDOC_AACTION_WC);
69   FPDFDOC_ExitFormFillEnvironment(form_handle);
70   FPDF_CloseDocument(doc);
71   FPDF_DestroyLibrary();
72 
73   isolate->Dispose();
74   v8::V8::ShutdownPlatform();
75   delete platform;
76 
77   return 0;
78 }
79