Saving pdf file using saveAsCopy()

209 views
Skip to first unread message

khazri achraf

unread,
Apr 6, 2020, 12:29:06 PM4/6/20
to pdfium
Is there any examples for saving a pdf file, the structur pdfwriter is not clear, please share any examples if you have .

Lei Zhang

unread,
Apr 6, 2020, 3:44:15 PM4/6/20
to khazri achraf, pdfium
Using a source search tool like https://source.chromium.org/chromium,
find the definition for FPDF_SaveAsCopy(). Then one can click on it to
see all the cross-references and see how it is used in the Chromium
code base, which includes a copy of PDFium. Similarly, one can click
on FPDF_FILEWRITE to see how it is used.

If the documentation for FPDF_FILEWRITE is insufficient, can you
elaborate on what is not clear?

On Mon, Apr 6, 2020 at 9:29 AM khazri achraf <achraf...@gmail.com> wrote:
>
> Is there any examples for saving a pdf file, the structur pdfwriter is not clear, please share any examples if you have .
>
> --
> You received this message because you are subscribed to the Google Groups "pdfium" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pdfium+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pdfium/c5fc6957-cb81-4fc6-99c9-64a6454aa84e%40googlegroups.com.

Lei Zhang

unread,
Apr 7, 2020, 4:36:05 PM4/7/20
to khazri achraf, pdfium
Also, please reply to the group, so others can be part of the conversation.

On Tue, Apr 7, 2020 at 1:35 PM Lei Zhang <the...@chromium.org> wrote:
>
> How about the sample call in pdf/pdfium/pdfium_print.cc? That uses an
> implementation, PDFiumMemBufferFileWrite, that meets all of the
> FPDF_FILEWRITE interface requirements.
>
> On Mon, Apr 6, 2020 at 1:09 PM khazri achraf <achraf...@gmail.com> wrote:
> >
> > Thanks for the reply Lei Zhang,
> >
> > Here is the path https://source.chromium.org/chromium/chromium/src/+/master:third_party/pdfium/public/fpdf_save.h
> > I want to create a fonction in C++ that takes as input pdf file and split each page apart and saving them all in an output folder, and for saving I need to use the function
> > FPDF_SaveAsCopy() and that function takes as argument FPDF_FILEWRITE struct type.
> > I have looked for examples but I didn't found a good one that describes how to use it for saving a FPDF_DOCUMENT.

khazri achraf

unread,
Apr 8, 2020, 4:29:31 AM4/8/20
to pdfium
I managed to save pdf file with the help of this test https://source.chromium.org/chromium/chromium/src/+/master:third_party/pdfium/fpdfsdk/fpdf_ppo_embeddertest.cpp

Here is the code that I used, once I finish the saving, I get damaget file, Any idea what the cause : 
```
std::string data_string_;
std::string saved_document_file_data_;
std::ofstream filestream_;

int FakeBlockWriter(FPDF_FILEWRITE* pThis, const void* pData, unsigned long size)
{
std::string data;
data.append(static_cast<const char*>(pData), size);

std::ofstream filestream_;
filestream_.open("pdfiumxx.txt", std::ios_base::binary);
if (filestream_.is_open()) filestream_.write(static_cast<const char*>(pData), size);
filestream_.close();

return size;

TEST_CASE("PDFIUM") {

FPDF_LIBRARY_CONFIG config;
config.version = 2;
config.m_pUserFontPaths = NULL;
config.m_pIsolate = NULL;
config.m_v8EmbedderSlot = 0;

FPDF_InitLibraryWithConfig(&config);

FPDF_DOCUMENT doc = FPDF_LoadDocument("test.pdf", NULL);

FPDF_FILEWRITE writer;
writer.version = 1;
writer.WriteBlock = FakeBlockWriter;
FPDF_SaveAsCopy(doc, &writer, 3);
FPDF_CloseDocument(doc);
}
```

khazri achraf

unread,
Apr 8, 2020, 4:29:31 AM4/8/20
to pdfium
Hi Lei Zhang,

I had a look on this file https://source.chromium.org/chromium/chromium/src/+/master:third_party/pdfium/fpdfsdk/fpdf_ppo_embeddertest.cpp and I guess I found the path how to use the saving method.
But after saving I am having damaged files, I just load the file and resave it nothing else, here is the test case that I made :

std::string data_string_;
std::string saved_document_file_data_;
std::ofstream filestream_;

int FakeBlockWriter(FPDF_FILEWRITE* pThis, const void* pData, unsigned long size)
{
std::string data;
data.append(static_cast<const char*>(pData), size);

std::ofstream filestream_;
filestream_.open("pdfiumxx.txt", std::ios_base::binary);
if (filestream_.is_open()) filestream_.write(static_cast<const char*>(pData), size);
filestream_.close();

return size;
}

TEST_CASE("PDFIUM") {


FPDF_LIBRARY_CONFIG config;
config.version = 2;
config.m_pUserFontPaths = NULL;
config.m_pIsolate = NULL;
config.m_v8EmbedderSlot = 0;

FPDF_InitLibraryWithConfig(&config);

FPDF_DOCUMENT doc = FPDF_LoadDocument("./datasets/document/converter/PdfSplitMerge/3.pdf", NULL);
FPDF_FILEWRITE writer;
writer.version = 1;
writer.WriteBlock = FakeBlockWriter;
FPDF_SaveAsCopy(doc, &writer, 3);
FPDF_CloseDocument(doc);

FPDF_DestroyLibrary();
> >> > To unsubscribe from this group and stop receiving emails from it, send an email to pdf...@googlegroups.com.

K. Moon

unread,
Apr 8, 2020, 1:22:08 PM4/8/20
to khazri achraf, pdfium
It looks like you're expecting the file to be written as a single block (open file, write block, close file). If more than one block needs to be written, you're going to just end up overwriting the previous blocks, which won't be a valid PDF file.

You need to open the file, append all of the blocks, and then close it.

To unsubscribe from this group and stop receiving emails from it, send an email to pdfium+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pdfium/ef340590-8760-484d-b24d-f3cabaf0ea2b%40googlegroups.com.

khazri achraf

unread,
Apr 9, 2020, 3:40:02 AM4/9/20
to pdfium
Sounds like I have to loop over blocks, I will try it.
 
Have you any programming guide or a link for a documentation ? I searched and nothing that I can find.

K. Moon

unread,
Apr 9, 2020, 12:13:30 PM4/9/20
to khazri achraf, pdfium
I believe the only API documentation are the comments in the public header files. There is probably example code in our test files that you can look at, too.

To unsubscribe from this group and stop receiving emails from it, send an email to pdfium+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pdfium/b743f9d2-fe3b-439d-ac4e-0d7c565c7eaa%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages