Rix is the official package layer for Vix.cpp Browse packages
Skip to content

Make Text

This example shows how to generate a simple text PDF with rix.pdf.make_text.

The example uses the public Rix facade:

cpp
#include <rix.hpp>

and accesses PDF through:

cpp
rix.pdf

Use this example when you only need a quick PDF with a title and plain text content.

Create the file

bash
mkdir -p ~/rix-pdf-make-text-example
cd ~/rix-pdf-make-text-example
touch make_text.cpp

Add:

cpp
#include <rix.hpp>

int main()
{
  auto saved = rix.pdf.make_text(
      "rix_pdf_make_text.pdf",
      "This file was generated with the high-level rix.pdf.make_text helper.",
      "Rix PDF");

  if (saved.failed())
  {
    rix.debug.eprint(
        "pdf error:",
        rix.pdf.error.to_string(saved.error()),
        saved.error().message());

    return 1;
  }

  rix.debug.print("created:", "rix_pdf_make_text.pdf");
  return 0;
}

Run it:

bash
vix run make_text.cpp

If Rix is not available yet for single-file usage:

bash
vix install -g rix/rix
vix run make_text.cpp

This creates:

txt
rix_pdf_make_text.pdf

What this example does

The example calls:

cpp
rix.pdf.make_text(...)

make_text creates a document, adds a page, writes the title and text content, and saves the PDF file.

It is the shortest PDF workflow in rix/pdf.

Function shape

Use:

cpp
auto saved = rix.pdf.make_text(
    "output.pdf",
    "Text content",
    "Optional title");

The arguments are:

txt
output file path
text content
optional title

The result is a PDF status.

Save path

The first argument is the output file path:

cpp
"rix_pdf_make_text.pdf"

The path must not be empty.

If the file is inside a folder, the folder must already exist.

Example:

bash
mkdir -p output

Then:

cpp
auto saved = rix.pdf.make_text(
    "output/hello.pdf",
    "Hello from rix.pdf",
    "Rix PDF");

Text content

The second argument is the content written inside the PDF:

cpp
"This file was generated with the high-level rix.pdf.make_text helper."

Use it for short plain-text content.

For complex layout, use rix.pdf.document() instead.

Optional title

The third argument is optional:

cpp
"Rix PDF"

When provided, it is used as the document title and visible heading.

You can also call make_text without a title:

cpp
auto saved = rix.pdf.make_text(
    "hello.pdf",
    "Hello from rix.pdf");

Check errors

make_text returns a status.

Always check it:

cpp
if (saved.failed())
{
  rix.debug.eprint(
      "pdf error:",
      rix.pdf.error.to_string(saved.error()),
      saved.error().message());

  return 1;
}

On success:

cpp
rix.debug.print("created:", "rix_pdf_make_text.pdf");

Complete minimal example

cpp
#include <rix.hpp>

int main()
{
  auto saved = rix.pdf.make_text(
      "hello.pdf",
      "Hello from rix.pdf",
      "Rix PDF");

  if (saved.failed())
  {
    rix.debug.eprint(
        "pdf error:",
        rix.pdf.error.to_string(saved.error()),
        saved.error().message());

    return 1;
  }

  rix.debug.print("created:", "hello.pdf");
  return 0;
}

Run:

bash
vix run make_text.cpp

Without a title

cpp
#include <rix.hpp>

int main()
{
  auto saved = rix.pdf.make_text(
      "plain.pdf",
      "This PDF has text content without a visible title.");

  if (saved.failed())
  {
    rix.debug.eprint(
        "pdf error:",
        rix.pdf.error.to_string(saved.error()),
        saved.error().message());

    return 1;
  }

  rix.debug.print("created:", "plain.pdf");
  return 0;
}

Run:

bash
vix run make_text.cpp

With a title

cpp
#include <rix.hpp>

int main()
{
  auto saved = rix.pdf.make_text(
      "with-title.pdf",
      "This PDF includes a title and body text.",
      "Generated Text PDF");

  if (saved.failed())
  {
    rix.debug.eprint(
        "pdf error:",
        rix.pdf.error.to_string(saved.error()),
        saved.error().message());

    return 1;
  }

  rix.debug.print("created:", "with-title.pdf");
  return 0;
}

The title is used for metadata and visible content.

When to use make_text

Use make_text when you need:

txt
a quick text-only PDF
a small generated note
a simple export
a small report summary
a minimal example

It is useful when the document does not need custom layout.

When not to use make_text

Use rix.pdf.document() when you need:

txt
multiple pages
tables
drawings
images
custom metadata
custom text styles
manual positioning
several sections

Example:

cpp
auto doc = rix.pdf.document();

auto &page = doc.add_page();

page.heading(
    page.x_left(),
    page.y_top(),
    "Custom PDF",
    1);

auto saved = rix.pdf.save(doc, "custom.pdf");

make_text vs document

make_text is short:

cpp
auto saved = rix.pdf.make_text(
    "hello.pdf",
    "Hello from rix.pdf",
    "Rix PDF");

document gives more control:

cpp
auto doc = rix.pdf.document();

doc.set_title("Rix PDF");

auto &page = doc.add_page();

page.heading(
    page.x_left(),
    page.y_top(),
    "Rix PDF",
    1);

page.paragraph(
    page.x_left(),
    page.y_top() - 50.0F,
    page.content_width(),
    "Hello from rix.pdf");

auto saved = rix.pdf.save(doc, "hello.pdf");

Choose make_text for speed.

Choose document for layout control.

Error example

An empty path fails:

cpp
#include <rix.hpp>

int main()
{
  auto saved = rix.pdf.make_text(
      "",
      "This should fail.",
      "Invalid path");

  if (saved.failed())
  {
    rix.debug.eprint(
        "expected pdf error:",
        rix.pdf.error.to_string(saved.error()),
        saved.error().message());

    return 0;
  }

  return 1;
}

Run:

bash
vix run make_text.cpp

The failure is expected because the output path is empty.

Missing folder example

This can fail if output/ does not exist:

cpp
auto saved = rix.pdf.make_text(
    "output/hello.pdf",
    "Hello from rix.pdf",
    "Rix PDF");

Create the folder first:

bash
mkdir -p output

Then run again.

Use in a Vix project

Create a project:

bash
vix new rix-pdf-make-text --app
cd rix-pdf-make-text

Add Rix:

bash
vix add rix/rix
vix install

Make sure vix.app contains:

txt
deps = [
  "rix/rix",
]

A minimal vix.app can look like this:

txt
name = "rix-pdf-make-text"
type = "executable"
standard = "c++20"
output_dir = "bin"

sources = [
  "src/main.cpp",
]

include_dirs = [
  "include",
  "src",
]

deps = [
  "rix/rix",
]

packages = [
  "vix",
]

links = [
  "vix::vix",
]

Put the example code in:

txt
src/main.cpp

Build and run:

bash
vix build
vix run

Single-file usage

For examples, tests, and quick experiments:

bash
vix run make_text.cpp

If needed:

bash
vix install -g rix/rix
vix run make_text.cpp

For project usage, prefer:

bash
vix add rix/rix
vix install

and keep the dependency in vix.app:

txt
deps = [
  "rix/rix",
]

Use only PDF with the facade

If you want the rix.* facade style but only want PDF mounted, define the feature macro before including rix.hpp:

cpp
#define RIX_ENABLE_PDF
#include <rix.hpp>

int main()
{
  auto saved = rix.pdf.make_text(
      "hello.pdf",
      "Hello from rix.pdf",
      "Rix PDF");

  return saved.ok() ? 0 : 1;
}

When at least one RIX_ENABLE_* macro is defined, only selected modules are mounted.

If you also want debug output:

cpp
#define RIX_ENABLE_PDF
#define RIX_ENABLE_DEBUG
#include <rix.hpp>

int main()
{
  auto saved = rix.pdf.make_text(
      "hello.pdf",
      "Hello from rix.pdf",
      "Rix PDF");

  if (saved.failed())
  {
    rix.debug.eprint(
        "pdf error:",
        rix.pdf.error.to_string(saved.error()),
        saved.error().message());

    return 1;
  }

  rix.debug.print("created:", "hello.pdf");
  return 0;
}

Use the independent package

For independent usage, install:

bash
vix add rix/pdf
vix install

In vix.app:

txt
deps = [
  "rix/pdf",
]

Then include:

cpp
#include <rix/pdf.hpp>

Example:

cpp
#include <rix/pdf.hpp>

int main()
{
  auto pdf = rixlib::pdf::module();

  auto saved = pdf.make_text(
      "hello.pdf",
      "Hello from rix/pdf",
      "Rix PDF");

  return saved.ok() ? 0 : 1;
}

The examples in this documentation prefer the public facade:

cpp
#include <rix.hpp>

and:

cpp
rix.pdf

Common mistakes

Forgetting to install Rix

If rix.hpp is not found, install Rix first.

For a project:

bash
vix add rix/rix
vix install

For single-file usage:

bash
vix install -g rix/rix

Putting Rix in packages

Wrong:

txt
packages = [
  "rix/rix",
]

Correct:

txt
deps = [
  "rix/rix",
]

deps is for Vix Registry packages.

packages is for CMake package discovery.

Using make_text for complex layout

make_text is for simple text PDFs.

For layout, tables, drawings, or custom styles, use:

cpp
auto doc = rix.pdf.document();

Saving to an empty path

This fails:

cpp
rix.pdf.make_text(
    "",
    "Hello",
    "Rix PDF");

Use a real path:

cpp
rix.pdf.make_text(
    "hello.pdf",
    "Hello",
    "Rix PDF");

Saving to a missing folder

This can fail:

cpp
rix.pdf.make_text(
    "output/hello.pdf",
    "Hello",
    "Rix PDF");

Create the folder first:

bash
mkdir -p output

Not checking save errors

Wrong:

cpp
rix.pdf.make_text(
    "hello.pdf",
    "Hello",
    "Rix PDF");

Correct:

cpp
auto saved = rix.pdf.make_text(
    "hello.pdf",
    "Hello",
    "Rix PDF");

if (saved.failed())
{
  rix.debug.eprint(
      "pdf error:",
      rix.pdf.error.to_string(saved.error()),
      saved.error().message());

  return 1;
}

What you should remember

Use make_text for a simple text PDF:

cpp
auto saved = rix.pdf.make_text(
    "hello.pdf",
    "Hello from rix.pdf",
    "Rix PDF");

Check errors:

cpp
if (saved.failed())
{
  rix.debug.eprint(
      "pdf error:",
      rix.pdf.error.to_string(saved.error()),
      saved.error().message());
}

Use document() for custom layout:

cpp
auto doc = rix.pdf.document();

For project usage:

bash
vix add rix/rix
vix install

and keep:

txt
deps = [
  "rix/rix",
]

Next step

Continue with PDF error handling.

Next: Error handling

Released under the MIT License.