aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-02-01 15:58:32 +0100
committerJay Berkenbilt <ejb@ql.org>2022-02-01 19:50:58 +0100
commita0d9d9923ce397d46680a9b180f253b39135ece2 (patch)
treee32ad4accca1ba83780b35cf74d6e2de6f2335d7 /examples
parentb02d37bc0ae0b7af6077637f855be8579c768c22 (diff)
downloadqpdf-a0d9d9923ce397d46680a9b180f253b39135ece2.tar.zst
Finish QPDFJob examples and add tests for them
Diffstat (limited to 'examples')
-rw-r--r--examples/build.mk3
-rw-r--r--examples/qpdf-job.cc93
-rw-r--r--examples/qpdfjob-c.c62
-rw-r--r--examples/qtest/qpdf-job.test32
-rw-r--r--examples/qtest/qpdf-job/in.pdf78
-rw-r--r--examples/qtest/qpdf-job/out3
-rw-r--r--examples/qtest/qpdf-job/out.pdfbin0 -> 1300 bytes
-rw-r--r--examples/qtest/qpdfjob-c.test29
-rw-r--r--examples/qtest/qpdfjob-c/in.pdf78
-rw-r--r--examples/qtest/qpdfjob-c/out.pdfbin0 -> 1300 bytes
10 files changed, 367 insertions, 11 deletions
diff --git a/examples/build.mk b/examples/build.mk
index b4366c1a..88fd164e 100644
--- a/examples/build.mk
+++ b/examples/build.mk
@@ -17,7 +17,8 @@ BINS_examples = \
qpdf-job
CBINS_examples = \
pdf-c-objects \
- pdf-linearize
+ pdf-linearize \
+ qpdfjob-c
TARGETS_examples = $(foreach B,$(BINS_examples) $(CBINS_examples),examples/$(OUTPUT_DIR)/$(call binname,$(B)))
diff --git a/examples/qpdf-job.cc b/examples/qpdf-job.cc
index 41ee8603..d1760897 100644
--- a/examples/qpdf-job.cc
+++ b/examples/qpdf-job.cc
@@ -1,20 +1,27 @@
#include <qpdf/QPDFJob.hh>
#include <qpdf/QUtil.hh>
+#include <qpdf/QIntC.hh>
#include <iostream>
#include <cstring>
-// QXXXQ describe
+// This program is a simple demonstration of different ways to use the
+// QPDFJob API.
static char const* whoami = 0;
-#if 0 // QXXXQ
-static void usage(std::string const& msg)
+static void usage()
{
- std::cerr << "Usage: " << whoami << " QXXXQ" << std::endl;
+ std::cerr
+ << "Usage: " << whoami << std::endl
+ << "This program linearizes the first page of in.pdf to out1.pdf,"
+ << " out2.pdf, and"
+ << std::endl
+ << " out3.pdf, each demonstrating a different way to use the"
+ << " QPDFJob API"
+ << std::endl;
exit(2);
}
-#endif
int main(int argc, char* argv[])
{
@@ -26,24 +33,90 @@ int main(int argc, char* argv[])
whoami += 3;
}
+ if (argc != 1)
+ {
+ usage();
+ }
+
+ // The examples below all catch std::exception. Note that
+ // QPDFUsage can be caught separately to report on errors in using
+ // the API itself. For CLI, this is command-line usage. For JSON
+ // or the API, it would be errors from the equivalent invocation.
+
+ // Note that staticId is used for testing only.
+
try
{
+ // Use the config API
QPDFJob j;
j.config()
- ->inputFile("/tmp/1.pdf")
- ->outputFile("/tmp/2.pdf")
+ ->inputFile("in.pdf")
+ ->outputFile("out1.pdf")
->pages()
- ->pageSpec(".", "1-z")
+ ->pageSpec(".", "1")
->endPages()
- ->qdf()
+ ->linearize()
+ ->staticId() // for testing only
->checkConfiguration();
j.run();
+ std::cout << "out1 status: " << j.getExitCode() << std::endl;
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << "exception: " << e.what() << std::endl;
+ return 2;
+ }
+
+ try
+ {
+ char const* new_argv[] = {
+ whoami,
+ "in.pdf",
+ "out2.pdf",
+ "--linearize",
+ "--pages",
+ ".",
+ "1",
+ "--",
+ "--static-id",
+ nullptr
+ };
+ QPDFJob j;
+ j.initializeFromArgv(9, new_argv);
+ j.run();
+ std::cout << "out2 status: " << j.getExitCode() << std::endl;
}
catch (std::exception& e)
{
- // QXXXQ catch usage, configerror, whatever we end up with separately
std::cerr << "exception: " << e.what() << std::endl;
return 2;
}
+
+ try
+ {
+ // Use the JSON API
+ QPDFJob j;
+ j.initializeFromJson(R"({
+ "inputFile": "in.pdf",
+ "outputFile": "out3.pdf",
+ "staticId": "",
+ "linearize": "",
+ "pages": [
+ {
+ "file": ".",
+ "range": "1"
+ }
+ ]
+}
+)");
+ j.run();
+ std::cout << "out3 status: " << j.getExitCode() << std::endl;
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << "exception: " << e.what() << std::endl;
+ return 2;
+ }
+
return 0;
}
diff --git a/examples/qpdfjob-c.c b/examples/qpdfjob-c.c
new file mode 100644
index 00000000..da53001a
--- /dev/null
+++ b/examples/qpdfjob-c.c
@@ -0,0 +1,62 @@
+/*
+ * This is an example program to linearize a PDF file using the C
+ * QPDFJob API.
+ */
+
+#include <qpdf/qpdfjob-c.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static char const* whoami = 0;
+
+static void usage()
+{
+ fprintf(stderr, "Usage: %s infile outfile\n", whoami);
+ exit(2);
+}
+
+int main(int argc, char* argv[])
+{
+ char* infile = NULL;
+ char* outfile = NULL;
+ char const* new_argv[6];
+ int r = 0;
+ char* p = 0;
+
+ if ((p = strrchr(argv[0], '/')) != NULL)
+ {
+ whoami = p + 1;
+ }
+ else if ((p = strrchr(argv[0], '\\')) != NULL)
+ {
+ whoami = p + 1;
+ }
+ else
+ {
+ whoami = argv[0];
+ }
+
+ if (argc != 3)
+ {
+ usage();
+ }
+
+ infile = argv[1];
+ outfile = argv[2];
+
+ new_argv[0] = "qpdfjob";
+ new_argv[1] = infile;
+ new_argv[2] = outfile;
+ new_argv[3] = "--linearize";
+ new_argv[4] = "--static-id"; /* for testing only */
+ new_argv[5] = NULL;
+
+ /* See qpdf-job.cc for a C++ example of using the json interface.
+ * To use that from C just like the argv one, call
+ * qpdfjob_run_from_json instead and pass the json string as a
+ * single char const* argument.
+ */
+ r = qpdfjob_run_from_argv(5, new_argv);
+ return r;
+}
diff --git a/examples/qtest/qpdf-job.test b/examples/qtest/qpdf-job.test
new file mode 100644
index 00000000..d3a490ab
--- /dev/null
+++ b/examples/qtest/qpdf-job.test
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+require 5.008;
+use warnings;
+use strict;
+
+chdir("qpdf-job") or die "chdir testdir failed: $!\n";
+
+require TestDriver;
+
+cleanup();
+
+my $td = new TestDriver('qpdf-job');
+
+$td->runtest("qpdf-job",
+ {$td->COMMAND => "qpdf-job"},
+ {$td->FILE => "out", $td->EXIT_STATUS => 0},
+ $td->NORMALIZE_NEWLINES);
+for (my $i = 1; $i <= 3; ++$i)
+{
+ $td->runtest("check output $i",
+ {$td->FILE => "out$i.pdf"},
+ {$td->FILE => "out.pdf"});
+}
+
+cleanup();
+
+$td->report(4);
+
+sub cleanup
+{
+ unlink "out1.pdf", "out2.pdf", "out3.pdf";
+}
diff --git a/examples/qtest/qpdf-job/in.pdf b/examples/qtest/qpdf-job/in.pdf
new file mode 100644
index 00000000..37457117
--- /dev/null
+++ b/examples/qtest/qpdf-job/in.pdf
@@ -0,0 +1,78 @@
+%PDF-1.3
+1 0 obj
+<<
+ /Type /Catalog
+ /Pages 2 0 R
+>>
+endobj
+
+2 0 obj
+<<
+ /Type /Pages
+ /Kids [
+ 3 0 R
+ ]
+ /Count 1
+>>
+endobj
+
+3 0 obj
+<<
+ /Type /Page
+ /Parent 2 0 R
+ /MediaBox [0 0 612 792]
+ /Contents 4 0 R
+ /Resources <<
+ /ProcSet 5 0 R
+ /Font <<
+ /F1 6 0 R
+ >>
+ >>
+>>
+endobj
+
+4 0 obj
+<<
+ /Length 44
+>>
+stream
+BT
+ /F1 24 Tf
+ 72 720 Td
+ (Potato) Tj
+ET
+endstream
+endobj
+
+5 0 obj
+[
+ /PDF
+ /Text
+]
+endobj
+
+6 0 obj
+<<
+ /Type /Font
+ /Subtype /Type1
+ /BaseFont /Helvetica
+ /Encoding /WinAnsiEncoding
+>>
+endobj
+
+xref
+0 7
+0000000000 65535 f
+0000000009 00000 n
+0000000063 00000 n
+0000000135 00000 n
+0000000307 00000 n
+0000000403 00000 n
+0000000438 00000 n
+trailer <<
+ /Size 7
+ /Root 1 0 R
+>>
+startxref
+544
+%%EOF
diff --git a/examples/qtest/qpdf-job/out b/examples/qtest/qpdf-job/out
new file mode 100644
index 00000000..e4d55650
--- /dev/null
+++ b/examples/qtest/qpdf-job/out
@@ -0,0 +1,3 @@
+out1 status: 0
+out2 status: 0
+out3 status: 0
diff --git a/examples/qtest/qpdf-job/out.pdf b/examples/qtest/qpdf-job/out.pdf
new file mode 100644
index 00000000..c432ac59
--- /dev/null
+++ b/examples/qtest/qpdf-job/out.pdf
Binary files differ
diff --git a/examples/qtest/qpdfjob-c.test b/examples/qtest/qpdfjob-c.test
new file mode 100644
index 00000000..33a55431
--- /dev/null
+++ b/examples/qtest/qpdfjob-c.test
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+require 5.008;
+use warnings;
+use strict;
+
+chdir("qpdf-job") or die "chdir testdir failed: $!\n";
+
+require TestDriver;
+
+cleanup();
+
+my $td = new TestDriver('qpdfjob-c');
+
+$td->runtest("qpdfjob-c",
+ {$td->COMMAND => "qpdfjob-c in.pdf a.pdf"},
+ {$td->STRING => "", $td->EXIT_STATUS => 0},
+ $td->NORMALIZE_NEWLINES);
+$td->runtest("check output",
+ {$td->FILE => "a.pdf"},
+ {$td->FILE => "out.pdf"});
+
+cleanup();
+
+$td->report(2);
+
+sub cleanup
+{
+ unlink "a.pdf";
+}
diff --git a/examples/qtest/qpdfjob-c/in.pdf b/examples/qtest/qpdfjob-c/in.pdf
new file mode 100644
index 00000000..37457117
--- /dev/null
+++ b/examples/qtest/qpdfjob-c/in.pdf
@@ -0,0 +1,78 @@
+%PDF-1.3
+1 0 obj
+<<
+ /Type /Catalog
+ /Pages 2 0 R
+>>
+endobj
+
+2 0 obj
+<<
+ /Type /Pages
+ /Kids [
+ 3 0 R
+ ]
+ /Count 1
+>>
+endobj
+
+3 0 obj
+<<
+ /Type /Page
+ /Parent 2 0 R
+ /MediaBox [0 0 612 792]
+ /Contents 4 0 R
+ /Resources <<
+ /ProcSet 5 0 R
+ /Font <<
+ /F1 6 0 R
+ >>
+ >>
+>>
+endobj
+
+4 0 obj
+<<
+ /Length 44
+>>
+stream
+BT
+ /F1 24 Tf
+ 72 720 Td
+ (Potato) Tj
+ET
+endstream
+endobj
+
+5 0 obj
+[
+ /PDF
+ /Text
+]
+endobj
+
+6 0 obj
+<<
+ /Type /Font
+ /Subtype /Type1
+ /BaseFont /Helvetica
+ /Encoding /WinAnsiEncoding
+>>
+endobj
+
+xref
+0 7
+0000000000 65535 f
+0000000009 00000 n
+0000000063 00000 n
+0000000135 00000 n
+0000000307 00000 n
+0000000403 00000 n
+0000000438 00000 n
+trailer <<
+ /Size 7
+ /Root 1 0 R
+>>
+startxref
+544
+%%EOF
diff --git a/examples/qtest/qpdfjob-c/out.pdf b/examples/qtest/qpdfjob-c/out.pdf
new file mode 100644
index 00000000..c432ac59
--- /dev/null
+++ b/examples/qtest/qpdfjob-c/out.pdf
Binary files differ