From a0d9d9923ce397d46680a9b180f253b39135ece2 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Tue, 1 Feb 2022 09:58:32 -0500 Subject: Finish QPDFJob examples and add tests for them --- examples/build.mk | 3 +- examples/qpdf-job.cc | 93 ++++++++++++++++++++++++++++++++++----- examples/qpdfjob-c.c | 62 ++++++++++++++++++++++++++ examples/qtest/qpdf-job.test | 32 ++++++++++++++ examples/qtest/qpdf-job/in.pdf | 78 ++++++++++++++++++++++++++++++++ examples/qtest/qpdf-job/out | 3 ++ examples/qtest/qpdf-job/out.pdf | Bin 0 -> 1300 bytes examples/qtest/qpdfjob-c.test | 29 ++++++++++++ examples/qtest/qpdfjob-c/in.pdf | 78 ++++++++++++++++++++++++++++++++ examples/qtest/qpdfjob-c/out.pdf | Bin 0 -> 1300 bytes 10 files changed, 367 insertions(+), 11 deletions(-) create mode 100644 examples/qpdfjob-c.c create mode 100644 examples/qtest/qpdf-job.test create mode 100644 examples/qtest/qpdf-job/in.pdf create mode 100644 examples/qtest/qpdf-job/out create mode 100644 examples/qtest/qpdf-job/out.pdf create mode 100644 examples/qtest/qpdfjob-c.test create mode 100644 examples/qtest/qpdfjob-c/in.pdf create mode 100644 examples/qtest/qpdfjob-c/out.pdf (limited to 'examples') 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 #include +#include #include #include -// 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 +#include +#include +#include + +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 Binary files /dev/null and b/examples/qtest/qpdf-job/out.pdf 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 Binary files /dev/null and b/examples/qtest/qpdfjob-c/out.pdf differ -- cgit v1.2.3-54-g00ecf