Add QPDFWriter method to output to FILE*

This commit is contained in:
Jay Berkenbilt 2012-07-05 21:22:27 -04:00
parent 3b5d72b946
commit 8705e2e8fc
4 changed files with 49 additions and 6 deletions

View File

@ -1,3 +1,9 @@
2012-07-05 Jay Berkenbilt <ejb@ql.org>
* Add QPDFWriter methods to write to an already open stdio FILE*.
Implementation and idea area based on contributions from Tobias
Hoffmann.
2012-07-04 Jay Berkenbilt <ejb@ql.org>
* Accept changes from Tobias Hoffmann: add public method

View File

@ -49,6 +49,14 @@ class QPDFWriter
// setOutputFilename() for details.
QPDF_DLL
QPDFWriter(QPDF& pdf, char const* filename);
// Create a QPDFWriter object that writes its output to an already
// open FILE*. This is equivalent to calling the first
// constructor and then calling setOutputFile(). See
// setOutputFile() for details.
QPDF_DLL
QPDFWriter(QPDF& pdf, char const* description, FILE* file, bool close_file);
QPDF_DLL
~QPDFWriter();
@ -66,6 +74,14 @@ class QPDFWriter
QPDF_DLL
void setOutputFilename(char const* filename);
// Write to the given FILE*, which must be opened by the caller.
// If close_file is true, QPDFWriter will close the file.
// Otherwise, the caller must close the file. The file does not
// need to be seekable; it will be written to in a single pass.
// It must be open in binary mode.
QPDF_DLL
void setOutputFile(char const* description, FILE* file, bool close_file);
// Indicate that QPDFWriter should create a memory buffer to
// contain the final PDF file. Obtain the memory by calling
// getBuffer().

View File

@ -34,6 +34,14 @@ QPDFWriter::QPDFWriter(QPDF& pdf, char const* filename) :
setOutputFilename(filename);
}
QPDFWriter::QPDFWriter(QPDF& pdf, char const* description,
FILE *file, bool close_file) :
pdf(pdf)
{
init();
setOutputFile(description, file, close_file);
}
void
QPDFWriter::init()
{
@ -79,21 +87,31 @@ QPDFWriter::~QPDFWriter()
void
QPDFWriter::setOutputFilename(char const* filename)
{
this->filename = filename;
char const* description = filename;
FILE* f = 0;
if (filename == 0)
{
this->filename = "standard output";
description = "standard output";
QTC::TC("qpdf", "QPDFWriter write to stdout");
file = stdout;
f = stdout;
QUtil::binary_stdout();
}
else
{
QTC::TC("qpdf", "QPDFWriter write to file");
file = QUtil::fopen_wrapper(std::string("open ") + filename,
fopen(filename, "wb+"));
f = QUtil::fopen_wrapper(std::string("open ") + filename,
fopen(filename, "wb+"));
close_file = true;
}
setOutputFile(description, f, close_file);
}
void
QPDFWriter::setOutputFile(char const* description, FILE* file, bool close_file)
{
this->filename = description;
this->file = file;
this->close_file = close_file;
Pipeline* p = new Pl_StdioFile("qpdf output", file);
to_delete.push_back(p);
initializePipelineStack(p);

View File

@ -728,7 +728,10 @@ void runtest(int n, char const* filename)
checkPageContents(pages[11], "New page 11");
checkPageContents(pages[12], "New page 12");
QPDFWriter w(pdf, "a.pdf");
// Exercise writing to FILE*
FILE* out = QUtil::fopen_wrapper(std::string("open a.pdf"),
fopen("a.pdf", "wb"));
QPDFWriter w(pdf, "FILE* a.pdf", out, true);
w.setStaticID(true);
w.setStreamDataMode(qpdf_s_preserve);
w.write();