Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fpdf_save_embeddertest.cpp
Go to the documentation of this file.
1// Copyright 2016 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#include <array>
6#include <string>
7
8#include "core/fxcrt/fx_string.h"
9#include "public/cpp/fpdf_scopers.h"
10#include "public/fpdf_edit.h"
11#include "public/fpdf_ppo.h"
12#include "public/fpdf_save.h"
13#include "public/fpdfview.h"
14#include "testing/embedder_test.h"
15#include "testing/embedder_test_constants.h"
16#include "testing/gmock/include/gmock/gmock-matchers.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19using testing::HasSubstr;
20using testing::Not;
21using testing::StartsWith;
22
24
26 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
27 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
28 EXPECT_THAT(GetString(), StartsWith("%PDF-1.7\r\n"));
29 EXPECT_EQ(805u, GetString().size());
30}
31
33 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
34 EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, 14));
35 EXPECT_THAT(GetString(), StartsWith("%PDF-1.4\r\n"));
36 EXPECT_EQ(805u, GetString().size());
37}
38
40 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
41 EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, -1));
42 EXPECT_THAT(GetString(), StartsWith("%PDF-1.7\r\n"));
43
44 ClearString();
45 EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, 0));
46 EXPECT_THAT(GetString(), StartsWith("%PDF-1.7\r\n"));
47
48 ClearString();
49 EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, 18));
50 EXPECT_THAT(GetString(), StartsWith("%PDF-1.7\r\n"));
51}
52
54 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
55 EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, FPDF_INCREMENTAL, 14));
56 // Version gets taken as-is from input document.
57 EXPECT_THAT(GetString(), StartsWith("%PDF-1.7\n%\xa0\xf2\xa4\xf4"));
58 // Additional output produced vs. non incremental.
59 EXPECT_EQ(985u, GetString().size());
60}
61
63 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
64 EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, FPDF_NO_INCREMENTAL, 14));
65 EXPECT_THAT(GetString(), StartsWith("%PDF-1.4\r\n"));
66 EXPECT_EQ(805u, GetString().size());
67}
68
70 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
71 EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, FPDF_REMOVE_SECURITY, 14));
72 EXPECT_THAT(GetString(), StartsWith("%PDF-1.4\r\n"));
73 EXPECT_EQ(805u, GetString().size());
74}
75
77 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
78 EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 999999, 14));
79 EXPECT_THAT(GetString(), StartsWith("%PDF-1.4\r\n"));
80 EXPECT_EQ(805u, GetString().size());
81}
82
84 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
85
86 ScopedEmbedderTestPage page = LoadScopedPage(0);
87 EXPECT_TRUE(page);
88
89 ScopedFPDFDocument output_doc(FPDF_CreateNewDocument());
90 EXPECT_TRUE(output_doc);
91 EXPECT_TRUE(FPDF_ImportPages(output_doc.get(), document(), "1", 0));
92 EXPECT_TRUE(FPDF_SaveAsCopy(output_doc.get(), this, 0));
93
94}
95
97 ASSERT_TRUE(OpenDocument("bug_42271133.pdf"));
98 ScopedEmbedderTestPage page = LoadScopedPage(0);
99 ASSERT_TRUE(page);
100
101 // Arbitrarily remove the first page object.
102 auto text_object = FPDFPage_GetObject(page.get(), 0);
103 ASSERT_TRUE(text_object);
104 ASSERT_TRUE(FPDFPage_RemoveObject(page.get(), text_object));
105 FPDFPageObj_Destroy(text_object);
106
107 // Regenerate dirty stream and save the document.
108 ASSERT_TRUE(FPDFPage_GenerateContent(page.get()));
109 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
110
111 // Reload saved document.
112 ASSERT_TRUE(OpenSavedDocument());
113 FPDF_PAGE saved_page = LoadSavedPage(0);
114 ASSERT_TRUE(saved_page);
115
116 // Assert path fill color is not changed to black.
117 auto path_obj = FPDFPage_GetObject(saved_page, 0);
118 ASSERT_TRUE(path_obj);
119 unsigned int r;
120 unsigned int g;
121 unsigned int b;
122 unsigned int a;
123 ASSERT_TRUE(FPDFPageObj_GetFillColor(path_obj, &r, &g, &b, &a));
124 EXPECT_EQ(180u, r);
125 EXPECT_EQ(180u, g);
126 EXPECT_EQ(180u, b);
127
128 CloseSavedPage(saved_page);
129 CloseSavedDocument();
130}
131
133 const int kPageCount = 3;
134 std::array<std::string, kPageCount> original_md5;
135
136 ASSERT_TRUE(OpenDocument("linearized.pdf"));
137 for (int i = 0; i < kPageCount; ++i) {
138 ScopedEmbedderTestPage page = LoadScopedPage(i);
139 ASSERT_TRUE(page);
140 ScopedFPDFBitmap bitmap = RenderLoadedPage(page.get());
141 EXPECT_EQ(612, FPDFBitmap_GetWidth(bitmap.get()));
142 EXPECT_EQ(792, FPDFBitmap_GetHeight(bitmap.get()));
143 original_md5[i] = HashBitmap(bitmap.get());
144 }
145
146 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
147 EXPECT_THAT(GetString(), StartsWith("%PDF-1.6\r\n"));
148 EXPECT_THAT(GetString(), HasSubstr("/Root "));
149 EXPECT_THAT(GetString(), HasSubstr("/Info "));
150 EXPECT_THAT(GetString(), HasSubstr("/Size 37"));
151 EXPECT_THAT(GetString(), HasSubstr("35 0 obj"));
152 EXPECT_THAT(GetString(), HasSubstr("36 0 obj"));
153 EXPECT_THAT(GetString(), Not(HasSubstr("37 0 obj")));
154 EXPECT_THAT(GetString(), Not(HasSubstr("38 0 obj")));
155 EXPECT_EQ(7996u, GetString().size());
156
157 // Make sure new document renders the same as the old one.
158 ASSERT_TRUE(OpenSavedDocument());
159 for (int i = 0; i < kPageCount; ++i) {
160 FPDF_PAGE page = LoadSavedPage(i);
161 ASSERT_TRUE(page);
162 ScopedFPDFBitmap bitmap = RenderSavedPage(page);
163 EXPECT_EQ(original_md5[i], HashBitmap(bitmap.get()));
164 CloseSavedPage(page);
165 }
166 CloseSavedDocument();
167}
168
170 ASSERT_TRUE(OpenDocument("jpx_lzw.pdf"));
171 ScopedEmbedderTestPage page = LoadScopedPage(0);
172 ASSERT_TRUE(page);
173 while (FPDFPage_CountObjects(page.get()) > 0) {
174 ScopedFPDFPageObject object(FPDFPage_GetObject(page.get(), 0));
175 ASSERT_TRUE(object);
176 ASSERT_TRUE(FPDFPage_RemoveObject(page.get(), object.get()));
177 }
178 ASSERT_TRUE(FPDFPage_GenerateContent(page.get()));
179
180 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
181
182 // The new document should render as empty.
183 ASSERT_TRUE(OpenSavedDocument());
184 FPDF_PAGE saved_page = LoadSavedPage(0);
185 ASSERT_TRUE(saved_page);
186 ScopedFPDFBitmap bitmap = RenderSavedPage(saved_page);
187 EXPECT_EQ(pdfium::kBlankPage612By792Checksum, HashBitmap(bitmap.get()));
188 CloseSavedPage(saved_page);
189 CloseSavedDocument();
190
191 EXPECT_THAT(GetString(), StartsWith("%PDF-1.7\r\n"));
192 EXPECT_THAT(GetString(), HasSubstr("/Root "));
193 EXPECT_THAT(GetString(), Not(HasSubstr("/Image")));
194 EXPECT_LT(GetString().size(), 600u);
195}
196
197#ifdef PDF_ENABLE_XFA
198TEST_F(FPDFSaveEmbedderTest, SaveXFADoc) {
199 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
200 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
201 EXPECT_THAT(GetString(), StartsWith("%PDF-1.7\r\n"));
202 ASSERT_TRUE(OpenSavedDocument());
203 // TODO(tsepez): check for XFA forms in document
204 CloseSavedDocument();
205}
206#endif // PDF_ENABLE_XFA
207
209 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
210 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
211 EXPECT_THAT(GetString(), HasSubstr("0000000000 65535 f\r\n"));
212 EXPECT_THAT(GetString(), Not(HasSubstr("0000000000 65536 f\r\n")));
213}
214
216 ASSERT_TRUE(OpenDocument("bug_905142.pdf"));
217 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
218 EXPECT_THAT(GetString(), HasSubstr("/Length 0"));
219}
220
221// Should not trigger a DCHECK() failure in CFX_FileBufferArchive.
222// Fails because the PDF is malformed.
224 ASSERT_TRUE(OpenDocument("bug_1328389.pdf"));
225 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
226 EXPECT_THAT(GetString(), HasSubstr("/Foo/"));
227}
FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV FPDF_CreateNewDocument()
FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPage_GetObject(FPDF_PAGE page, int index)
#define FPDF_INCREMENTAL
Definition fpdf_save.h:45
#define FPDF_REMOVE_SECURITY
Definition fpdf_save.h:47
#define FPDF_NO_INCREMENTAL
Definition fpdf_save.h:46
TEST_F(FPDFSaveEmbedderTest, SaveSimpleDoc)
const char kBlankPage612By792Checksum[]