Skip to content

State Saving

galkahana edited this page Aug 15, 2011 · 2 revisions

Sometimes you'll want to stop using a certain instance of PDFWriter for the time being and later use another one to continue writing the PDF. This makes sense for such cases as:

  • Temporarily shutting down a service to free up memory, and later starting it again
  • Passing the work to a less loaded server
  • Writing a stateless console for writing PDFs

For such usages the library has shutdown/restart features implemented. The PDFWriter has two relevant functions:

EStatusCode Shutdown(const string& inStateFilePath);

EStatusCode ContinuePDF(const string& inOutputFilePath,
			const string& inStateFilePath,
			const LogConfiguration& inLogConfiguration = LogConfiguration::DefaultLogConfiguration);

Shutdown should be used instead of EndPDF when you wish to stop using this instance of PDFWriter, as the last call. Provide a file path for saving state information. File paths, as always, should UTf8 encoded.

For the new instance, use ContinuePDF instead of StartPDF. 3 Parameters should be provided:
inOutputFilePath - PDF output file path. You may have moved the file, so just pass wherever it is now, even if the same path.
inStateFilePath - State file path. should be the file created by the Shutdown method.
inLogConfiguration - Optionally you can select the log configuration for this context. The settings from the original usage are not be kept.

A simple usage example can be seen here:

PDFWriter pdfWriter;
pdfWriterA.StartPDF("C:\\MyPDF.PDF",ePDFVersion13);

/// write some PDF writing code with pdfWriterA

pdfWriterA.Shutdown("C:\\ShutDownRestartState.txt");

PDFWriter pdfWriterB;
pdfWriterB.ContinuePDF("C:\\MyPDF.PDF",L"C:\\ShutDownRestartState.txt");

// write some more PDF writing code, now with pdfWriterB

pdfWriterB.EndPDF();

quite obviously (as would be the whole point), you don't have to have the calls in the same code. Note also that the positions of the files (state file and output file) do not have to be the same for Shutdown and ContinuePDF.

Note that the state writing handles only the internal state of the PDFWriter, not whatever objects you allocated through the session, so make sure you finish writing whatever page you are writing, or Form XObject before going Shutdown.

Some technical internal detail that might be of interest to you - i'm using the PDF format itself for writing/reading state information - got a parser and writer...figured i might as well use them.

Clone this wiki locally