Skip to content

Instantly share code, notes, and snippets.

@sahilarora535
Created August 1, 2017 12:37
Show Gist options
  • Save sahilarora535/4c6ee8fa773d42b8b556568decdd396b to your computer and use it in GitHub Desktop.
Save sahilarora535/4c6ee8fa773d42b8b556568decdd396b to your computer and use it in GitHub Desktop.
Code to generate a PCLm file using QPDF
#include <bits/stdc++.h>
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFObjectHandle.hh>
#include <qpdf/QPDFWriter.hh>
int main() {
QPDF pdf;
pdf.emptyPDF();
QPDFObjectHandle unreferenced_obj = QPDFObjectHandle::newStream(&pdf);
QPDFObjectHandle page = QPDFObjectHandle::parse(
"<<"
" /Type /Page"
" /Resources <<"
" /XObject << >> "
" >>"
" /MediaBox null "
" /Contents null "
">>");
page.replaceKey("/Contents",QPDFObjectHandle::newStream(&pdf, "PAGE CONTENT STREAM\n")); // data will be provided later
pdf.addPage(page, false);
unreferenced_obj.replaceStreamData("UNREFERENCED STREAM\n",QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull());
QPDFWriter output(pdf, NULL);
output.setPCLm(true);
output.write();
return 0;
}
@sahilarora535
Copy link
Author

The PCLm produced by this does not contain the unreferenced stream. The final PCLm produced looks like this:

%PDF-1.3
%PCLm-1.0
1 0 obj
<< /Pages 2 0 R /Type /Catalog >>
endobj
2 0 obj
<< /Count 1 /Kids [ 3 0 R ] /Type /Pages >>
endobj
3 0 obj
<< /Contents 4 0 R /Parent 2 0 R /Resources << /XObject << >> >> /Type /Page >>
endobj
4 0 obj
<< /Length 20 >>
stream
PAGE CONTENT STREAM
endstream
endobj
xref
0 5
0000000000 65535 f 
0000000019 00000 n 
0000000068 00000 n 
0000000127 00000 n 
0000000222 00000 n 
trailer << /Root 1 0 R /Size 5 /ID [<cc06dab17f797447a472ee7a6bb96b38><cc06dab17f797447a472ee7a6bb96b38>] >>
startxref
291
%%EOF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment