Commit a0d9d9923ce397d46680a9b180f253b39135ece2
1 parent
b02d37bc
Finish QPDFJob examples and add tests for them
Showing
11 changed files
with
368 additions
and
12 deletions
examples/build.mk
examples/qpdf-job.cc
| 1 | 1 | #include <qpdf/QPDFJob.hh> |
| 2 | 2 | #include <qpdf/QUtil.hh> |
| 3 | +#include <qpdf/QIntC.hh> | |
| 3 | 4 | |
| 4 | 5 | #include <iostream> |
| 5 | 6 | #include <cstring> |
| 6 | 7 | |
| 7 | -// QXXXQ describe | |
| 8 | +// This program is a simple demonstration of different ways to use the | |
| 9 | +// QPDFJob API. | |
| 8 | 10 | |
| 9 | 11 | static char const* whoami = 0; |
| 10 | 12 | |
| 11 | -#if 0 // QXXXQ | |
| 12 | -static void usage(std::string const& msg) | |
| 13 | +static void usage() | |
| 13 | 14 | { |
| 14 | - std::cerr << "Usage: " << whoami << " QXXXQ" << std::endl; | |
| 15 | + std::cerr | |
| 16 | + << "Usage: " << whoami << std::endl | |
| 17 | + << "This program linearizes the first page of in.pdf to out1.pdf," | |
| 18 | + << " out2.pdf, and" | |
| 19 | + << std::endl | |
| 20 | + << " out3.pdf, each demonstrating a different way to use the" | |
| 21 | + << " QPDFJob API" | |
| 22 | + << std::endl; | |
| 15 | 23 | exit(2); |
| 16 | 24 | } |
| 17 | -#endif | |
| 18 | 25 | |
| 19 | 26 | int main(int argc, char* argv[]) |
| 20 | 27 | { |
| ... | ... | @@ -26,24 +33,90 @@ int main(int argc, char* argv[]) |
| 26 | 33 | whoami += 3; |
| 27 | 34 | } |
| 28 | 35 | |
| 36 | + if (argc != 1) | |
| 37 | + { | |
| 38 | + usage(); | |
| 39 | + } | |
| 40 | + | |
| 41 | + // The examples below all catch std::exception. Note that | |
| 42 | + // QPDFUsage can be caught separately to report on errors in using | |
| 43 | + // the API itself. For CLI, this is command-line usage. For JSON | |
| 44 | + // or the API, it would be errors from the equivalent invocation. | |
| 45 | + | |
| 46 | + // Note that staticId is used for testing only. | |
| 47 | + | |
| 29 | 48 | try |
| 30 | 49 | { |
| 50 | + // Use the config API | |
| 31 | 51 | QPDFJob j; |
| 32 | 52 | j.config() |
| 33 | - ->inputFile("/tmp/1.pdf") | |
| 34 | - ->outputFile("/tmp/2.pdf") | |
| 53 | + ->inputFile("in.pdf") | |
| 54 | + ->outputFile("out1.pdf") | |
| 35 | 55 | ->pages() |
| 36 | - ->pageSpec(".", "1-z") | |
| 56 | + ->pageSpec(".", "1") | |
| 37 | 57 | ->endPages() |
| 38 | - ->qdf() | |
| 58 | + ->linearize() | |
| 59 | + ->staticId() // for testing only | |
| 39 | 60 | ->checkConfiguration(); |
| 40 | 61 | j.run(); |
| 62 | + std::cout << "out1 status: " << j.getExitCode() << std::endl; | |
| 63 | + } | |
| 64 | + catch (std::exception& e) | |
| 65 | + { | |
| 66 | + std::cerr << "exception: " << e.what() << std::endl; | |
| 67 | + return 2; | |
| 68 | + } | |
| 69 | + | |
| 70 | + try | |
| 71 | + { | |
| 72 | + char const* new_argv[] = { | |
| 73 | + whoami, | |
| 74 | + "in.pdf", | |
| 75 | + "out2.pdf", | |
| 76 | + "--linearize", | |
| 77 | + "--pages", | |
| 78 | + ".", | |
| 79 | + "1", | |
| 80 | + "--", | |
| 81 | + "--static-id", | |
| 82 | + nullptr | |
| 83 | + }; | |
| 84 | + QPDFJob j; | |
| 85 | + j.initializeFromArgv(9, new_argv); | |
| 86 | + j.run(); | |
| 87 | + std::cout << "out2 status: " << j.getExitCode() << std::endl; | |
| 41 | 88 | } |
| 42 | 89 | catch (std::exception& e) |
| 43 | 90 | { |
| 44 | - // QXXXQ catch usage, configerror, whatever we end up with separately | |
| 45 | 91 | std::cerr << "exception: " << e.what() << std::endl; |
| 46 | 92 | return 2; |
| 47 | 93 | } |
| 94 | + | |
| 95 | + try | |
| 96 | + { | |
| 97 | + // Use the JSON API | |
| 98 | + QPDFJob j; | |
| 99 | + j.initializeFromJson(R"({ | |
| 100 | + "inputFile": "in.pdf", | |
| 101 | + "outputFile": "out3.pdf", | |
| 102 | + "staticId": "", | |
| 103 | + "linearize": "", | |
| 104 | + "pages": [ | |
| 105 | + { | |
| 106 | + "file": ".", | |
| 107 | + "range": "1" | |
| 108 | + } | |
| 109 | + ] | |
| 110 | +} | |
| 111 | +)"); | |
| 112 | + j.run(); | |
| 113 | + std::cout << "out3 status: " << j.getExitCode() << std::endl; | |
| 114 | + } | |
| 115 | + catch (std::exception& e) | |
| 116 | + { | |
| 117 | + std::cerr << "exception: " << e.what() << std::endl; | |
| 118 | + return 2; | |
| 119 | + } | |
| 120 | + | |
| 48 | 121 | return 0; |
| 49 | 122 | } | ... | ... |
examples/qpdfjob-c.c
0 → 100644
| 1 | +/* | |
| 2 | + * This is an example program to linearize a PDF file using the C | |
| 3 | + * QPDFJob API. | |
| 4 | + */ | |
| 5 | + | |
| 6 | +#include <qpdf/qpdfjob-c.h> | |
| 7 | +#include <stdio.h> | |
| 8 | +#include <stdlib.h> | |
| 9 | +#include <string.h> | |
| 10 | + | |
| 11 | +static char const* whoami = 0; | |
| 12 | + | |
| 13 | +static void usage() | |
| 14 | +{ | |
| 15 | + fprintf(stderr, "Usage: %s infile outfile\n", whoami); | |
| 16 | + exit(2); | |
| 17 | +} | |
| 18 | + | |
| 19 | +int main(int argc, char* argv[]) | |
| 20 | +{ | |
| 21 | + char* infile = NULL; | |
| 22 | + char* outfile = NULL; | |
| 23 | + char const* new_argv[6]; | |
| 24 | + int r = 0; | |
| 25 | + char* p = 0; | |
| 26 | + | |
| 27 | + if ((p = strrchr(argv[0], '/')) != NULL) | |
| 28 | + { | |
| 29 | + whoami = p + 1; | |
| 30 | + } | |
| 31 | + else if ((p = strrchr(argv[0], '\\')) != NULL) | |
| 32 | + { | |
| 33 | + whoami = p + 1; | |
| 34 | + } | |
| 35 | + else | |
| 36 | + { | |
| 37 | + whoami = argv[0]; | |
| 38 | + } | |
| 39 | + | |
| 40 | + if (argc != 3) | |
| 41 | + { | |
| 42 | + usage(); | |
| 43 | + } | |
| 44 | + | |
| 45 | + infile = argv[1]; | |
| 46 | + outfile = argv[2]; | |
| 47 | + | |
| 48 | + new_argv[0] = "qpdfjob"; | |
| 49 | + new_argv[1] = infile; | |
| 50 | + new_argv[2] = outfile; | |
| 51 | + new_argv[3] = "--linearize"; | |
| 52 | + new_argv[4] = "--static-id"; /* for testing only */ | |
| 53 | + new_argv[5] = NULL; | |
| 54 | + | |
| 55 | + /* See qpdf-job.cc for a C++ example of using the json interface. | |
| 56 | + * To use that from C just like the argv one, call | |
| 57 | + * qpdfjob_run_from_json instead and pass the json string as a | |
| 58 | + * single char const* argument. | |
| 59 | + */ | |
| 60 | + r = qpdfjob_run_from_argv(5, new_argv); | |
| 61 | + return r; | |
| 62 | +} | ... | ... |
examples/qtest/qpdf-job.test
0 → 100644
| 1 | +#!/usr/bin/env perl | |
| 2 | +require 5.008; | |
| 3 | +use warnings; | |
| 4 | +use strict; | |
| 5 | + | |
| 6 | +chdir("qpdf-job") or die "chdir testdir failed: $!\n"; | |
| 7 | + | |
| 8 | +require TestDriver; | |
| 9 | + | |
| 10 | +cleanup(); | |
| 11 | + | |
| 12 | +my $td = new TestDriver('qpdf-job'); | |
| 13 | + | |
| 14 | +$td->runtest("qpdf-job", | |
| 15 | + {$td->COMMAND => "qpdf-job"}, | |
| 16 | + {$td->FILE => "out", $td->EXIT_STATUS => 0}, | |
| 17 | + $td->NORMALIZE_NEWLINES); | |
| 18 | +for (my $i = 1; $i <= 3; ++$i) | |
| 19 | +{ | |
| 20 | + $td->runtest("check output $i", | |
| 21 | + {$td->FILE => "out$i.pdf"}, | |
| 22 | + {$td->FILE => "out.pdf"}); | |
| 23 | +} | |
| 24 | + | |
| 25 | +cleanup(); | |
| 26 | + | |
| 27 | +$td->report(4); | |
| 28 | + | |
| 29 | +sub cleanup | |
| 30 | +{ | |
| 31 | + unlink "out1.pdf", "out2.pdf", "out3.pdf"; | |
| 32 | +} | ... | ... |
examples/qtest/qpdf-job/in.pdf
0 → 100644
| 1 | +%PDF-1.3 | |
| 2 | +1 0 obj | |
| 3 | +<< | |
| 4 | + /Type /Catalog | |
| 5 | + /Pages 2 0 R | |
| 6 | +>> | |
| 7 | +endobj | |
| 8 | + | |
| 9 | +2 0 obj | |
| 10 | +<< | |
| 11 | + /Type /Pages | |
| 12 | + /Kids [ | |
| 13 | + 3 0 R | |
| 14 | + ] | |
| 15 | + /Count 1 | |
| 16 | +>> | |
| 17 | +endobj | |
| 18 | + | |
| 19 | +3 0 obj | |
| 20 | +<< | |
| 21 | + /Type /Page | |
| 22 | + /Parent 2 0 R | |
| 23 | + /MediaBox [0 0 612 792] | |
| 24 | + /Contents 4 0 R | |
| 25 | + /Resources << | |
| 26 | + /ProcSet 5 0 R | |
| 27 | + /Font << | |
| 28 | + /F1 6 0 R | |
| 29 | + >> | |
| 30 | + >> | |
| 31 | +>> | |
| 32 | +endobj | |
| 33 | + | |
| 34 | +4 0 obj | |
| 35 | +<< | |
| 36 | + /Length 44 | |
| 37 | +>> | |
| 38 | +stream | |
| 39 | +BT | |
| 40 | + /F1 24 Tf | |
| 41 | + 72 720 Td | |
| 42 | + (Potato) Tj | |
| 43 | +ET | |
| 44 | +endstream | |
| 45 | +endobj | |
| 46 | + | |
| 47 | +5 0 obj | |
| 48 | +[ | |
| 49 | ||
| 50 | + /Text | |
| 51 | +] | |
| 52 | +endobj | |
| 53 | + | |
| 54 | +6 0 obj | |
| 55 | +<< | |
| 56 | + /Type /Font | |
| 57 | + /Subtype /Type1 | |
| 58 | + /BaseFont /Helvetica | |
| 59 | + /Encoding /WinAnsiEncoding | |
| 60 | +>> | |
| 61 | +endobj | |
| 62 | + | |
| 63 | +xref | |
| 64 | +0 7 | |
| 65 | +0000000000 65535 f | |
| 66 | +0000000009 00000 n | |
| 67 | +0000000063 00000 n | |
| 68 | +0000000135 00000 n | |
| 69 | +0000000307 00000 n | |
| 70 | +0000000403 00000 n | |
| 71 | +0000000438 00000 n | |
| 72 | +trailer << | |
| 73 | + /Size 7 | |
| 74 | + /Root 1 0 R | |
| 75 | +>> | |
| 76 | +startxref | |
| 77 | +544 | |
| 78 | +%%EOF | ... | ... |
examples/qtest/qpdf-job/out
0 → 100644
examples/qtest/qpdf-job/out.pdf
0 → 100644
No preview for this file type
examples/qtest/qpdfjob-c.test
0 → 100644
| 1 | +#!/usr/bin/env perl | |
| 2 | +require 5.008; | |
| 3 | +use warnings; | |
| 4 | +use strict; | |
| 5 | + | |
| 6 | +chdir("qpdf-job") or die "chdir testdir failed: $!\n"; | |
| 7 | + | |
| 8 | +require TestDriver; | |
| 9 | + | |
| 10 | +cleanup(); | |
| 11 | + | |
| 12 | +my $td = new TestDriver('qpdfjob-c'); | |
| 13 | + | |
| 14 | +$td->runtest("qpdfjob-c", | |
| 15 | + {$td->COMMAND => "qpdfjob-c in.pdf a.pdf"}, | |
| 16 | + {$td->STRING => "", $td->EXIT_STATUS => 0}, | |
| 17 | + $td->NORMALIZE_NEWLINES); | |
| 18 | +$td->runtest("check output", | |
| 19 | + {$td->FILE => "a.pdf"}, | |
| 20 | + {$td->FILE => "out.pdf"}); | |
| 21 | + | |
| 22 | +cleanup(); | |
| 23 | + | |
| 24 | +$td->report(2); | |
| 25 | + | |
| 26 | +sub cleanup | |
| 27 | +{ | |
| 28 | + unlink "a.pdf"; | |
| 29 | +} | ... | ... |
examples/qtest/qpdfjob-c/in.pdf
0 → 100644
| 1 | +%PDF-1.3 | |
| 2 | +1 0 obj | |
| 3 | +<< | |
| 4 | + /Type /Catalog | |
| 5 | + /Pages 2 0 R | |
| 6 | +>> | |
| 7 | +endobj | |
| 8 | + | |
| 9 | +2 0 obj | |
| 10 | +<< | |
| 11 | + /Type /Pages | |
| 12 | + /Kids [ | |
| 13 | + 3 0 R | |
| 14 | + ] | |
| 15 | + /Count 1 | |
| 16 | +>> | |
| 17 | +endobj | |
| 18 | + | |
| 19 | +3 0 obj | |
| 20 | +<< | |
| 21 | + /Type /Page | |
| 22 | + /Parent 2 0 R | |
| 23 | + /MediaBox [0 0 612 792] | |
| 24 | + /Contents 4 0 R | |
| 25 | + /Resources << | |
| 26 | + /ProcSet 5 0 R | |
| 27 | + /Font << | |
| 28 | + /F1 6 0 R | |
| 29 | + >> | |
| 30 | + >> | |
| 31 | +>> | |
| 32 | +endobj | |
| 33 | + | |
| 34 | +4 0 obj | |
| 35 | +<< | |
| 36 | + /Length 44 | |
| 37 | +>> | |
| 38 | +stream | |
| 39 | +BT | |
| 40 | + /F1 24 Tf | |
| 41 | + 72 720 Td | |
| 42 | + (Potato) Tj | |
| 43 | +ET | |
| 44 | +endstream | |
| 45 | +endobj | |
| 46 | + | |
| 47 | +5 0 obj | |
| 48 | +[ | |
| 49 | ||
| 50 | + /Text | |
| 51 | +] | |
| 52 | +endobj | |
| 53 | + | |
| 54 | +6 0 obj | |
| 55 | +<< | |
| 56 | + /Type /Font | |
| 57 | + /Subtype /Type1 | |
| 58 | + /BaseFont /Helvetica | |
| 59 | + /Encoding /WinAnsiEncoding | |
| 60 | +>> | |
| 61 | +endobj | |
| 62 | + | |
| 63 | +xref | |
| 64 | +0 7 | |
| 65 | +0000000000 65535 f | |
| 66 | +0000000009 00000 n | |
| 67 | +0000000063 00000 n | |
| 68 | +0000000135 00000 n | |
| 69 | +0000000307 00000 n | |
| 70 | +0000000403 00000 n | |
| 71 | +0000000438 00000 n | |
| 72 | +trailer << | |
| 73 | + /Size 7 | |
| 74 | + /Root 1 0 R | |
| 75 | +>> | |
| 76 | +startxref | |
| 77 | +544 | |
| 78 | +%%EOF | ... | ... |
examples/qtest/qpdfjob-c/out.pdf
0 → 100644
No preview for this file type
include/qpdf/qpdfjob-c.h