libart-paperback 0.1.0-a.1.20260122225059.e5fea306241a
PDF (1.4) library for C++
Getting Started

Getting Started

Paperback uses the build2 build system, see https://build2.org/.

The first step is to include Paperback in your build2 project.

Add the following lines to your repositories.manifest file:

:
role: prerequisite
location: https://code.helloryan.se/art/libart-paperback.git##HEAD

And the following to your manifest file:

depends: libart-paperback ^0.1.0-

Finally, link your executable or library with Paperback:

import libs =+ libart-paperback%lib{art-paperback}

Usage Example

The example below shows how to use Paperback to create a new PDF document and render some text on a page. More examples are available in $src_root$/examples.

#include <fstream>
#include <iostream>
#include <art/paperback/document.hxx>
#include <art/paperback/page.hxx>
#include <art/paperback/graphics/canvas.hxx>
#include <art/paperback/graphics/standard-font.hxx>
using namespace std;
using namespace Art::Paperback;
using namespace Art::Paperback::Graphics;
int
main(int argc, char* argv[])
{
if (argc != 2) {
cerr << "usage: " << argv[0] << " <path>\n";
return 1;
}
try {
// Open the output file.
//
fstream fs{argv[1], std::ios_base::out | std::ios_base::binary};
// Create a new document.
//
Standard_font helvetica{document, "Helvetica"};
// Create a new A4 page.
//
double width = 595;
double height = 842;
auto& page = document.create_page(Page::Properties{
Rectangle{0, 0, width, height}
});
// Create a page canvas and render some text.
//
{
Canvas canvas{Canvas::clear, page};
Canvas::Set_font use_font{canvas, helvetica, 14};
// Measure the width of the text, so we can center it.
//
auto tw = helvetica.get_text_width("Hello, world!");
auto text_width = (tw.width * 14) / 1000;
auto capheight = (helvetica.get_capheight() * 14) / 1000;
double cx = (width / 2) - (text_width / 2);
double cy = (height / 2) - (capheight / 2);
Canvas::Begin_text bt{canvas};
bt.move_text_pos(cx, cy);
bt.show_text("Hello, world!");
}
// Write the document explicitly to the underlying I/O stream.
//
// Otherwise, the Document destructor flushes it automatically,
// ignoring any exceptions thrown.
//
document.flush();
}
catch (exception const& ex) {
cerr << argv[0] << ": an error occurred: " << ex.what() << "\n";
return 1;
}
catch (...) {
cerr << argv[0] << ": an unknown error occurred\n";
return 1;
}
return 0;
}
Definition document.hxx:12
static constexpr Create_new const create_new
Tag used to indicate the creation of a new document.
Definition document.hxx:18
Represents the drawable canvas of a page.
Definition canvas.hxx:15
static constexpr Clear const clear
Dispatch-tag used to clear a canvas.
Definition canvas.hxx:21
Represents a standard PDF font.
Definition standard-font.hxx:16
Represents a PDF rectangle.
Definition primitives.hxx:27
Graphics namespace.
Definition forward.hxx:105
Primary paperback namespace.
Definition array.cxx:6
Document & document(Page &page)
Get the document associated with a page.
Definition page.cxx:120
Properties of a new page.
Definition page.hxx:20