8 #include <qpdf/QPDFWriter.hh>
13 string page_out::new_resource(const string type)
15 return "/" + type + to_string(++res_cnt);
18 class in_page : public page {
19 QPDFObjectHandle pdf_page;
22 void render(page_out *out, pdf_matrix xform);
23 in_page(QPDFObjectHandle inpg, int idx);
26 in_page::in_page(QPDFObjectHandle inpg, int idx)
30 media_box = BBox(inpg.getKey("/MediaBox"));
31 width = media_box.width();
32 height = media_box.height();
35 void in_page::render(page_out *out, pdf_matrix xform)
37 // Convert page to xobject
38 QPDFObjectHandle page_copy = page_to_xobject(&out_pdf, out_pdf.copyForeignObject(pdf_page));
39 string xobj_res = out->new_resource("XO");
40 out->xobjects.replaceKey(xobj_res, out_pdf.makeIndirectObject(page_copy));
43 m.shift(-media_box.x_min, -media_box.y_min);
46 out->contents += "q " + m.to_string() + " cm " + xobj_res + " Do Q";
49 static void debug_pages(vector<page *> &pages)
55 debug("Page #%d: w=%.3f h=%.3f", pg->index, pg->width, pg->height);
58 static void process(list<cmd *> &cmds, const char *in_name, const char *out_name)
60 in_pdf.processFile(in_name);
61 in_pdf.pushInheritedAttributesToPage();
64 vector<QPDFObjectHandle> const &in_pages = in_pdf.getAllPages();
67 QPDFObjectHandle page_copy = out_pdf.copyForeignObject(in_pages[0]);
70 for (auto inpg: in_pages)
71 pages.push_back(new in_page(inpg, ++cnt));
72 debug("# Input document");
77 debug("# Executing %s", c->def->name);
78 pages = c->exec->process(pages);
85 out.resources = QPDFObjectHandle::newDictionary();
86 // FIXME: What if the source page requires a broader ProcSet?
87 out.resources.replaceKey("/ProcSet", QPDFObjectHandle::parse("[/PDF /Text]"));
88 out.xobjects = QPDFObjectHandle::newDictionary();
89 out.resources.replaceKey("/XObject", out.xobjects);
90 pg->render(&out, pdf_matrix());
92 QPDFObjectHandle contents = QPDFObjectHandle::newStream(&out_pdf, out.contents);
94 // Create the page object
95 QPDFObjectHandle out_page = out_pdf.makeIndirectObject(QPDFObjectHandle::newDictionary());
96 out_page.replaceKey("/Type", QPDFObjectHandle::newName("/Page"));
97 out_page.replaceKey("/MediaBox", BBox(pg->width, pg->height).to_array());
98 out_page.replaceKey("/Contents", contents);
99 out_page.replaceKey("/Resources", out.resources);
100 out_pdf.addPage(out_page, false);
103 // Produce info dictionary
104 QPDFObjectHandle trailer = out_pdf.getTrailer();
105 QPDFObjectHandle info = trailer.getKey("/Info");
108 info = QPDFObjectHandle::newDictionary();
109 trailer.replaceKey("/Info", info);
112 assert(info.isDictionary());
113 // FIXME: More meta-data
114 info.replaceKey("/Producer", unicode_string("PaperJam"));
116 // Write the output file
117 QPDFWriter writer(out_pdf, out_name);
121 int main(int argc, char **argv)
125 fprintf(stderr, "Usage: paperjam <commands> <input> <output>\n");
132 parse(argv[1], cmds);
136 process(cmds, argv[2], argv[3]);