How to print table of contents of a pdf?












2















I have a pdf kinda-book file which has a table of contents as metadata in file but they are not listed on any page of the document. I want to print the file with table of contents, or print the table of contents separately. How can I do that?










share|improve this question



























    2















    I have a pdf kinda-book file which has a table of contents as metadata in file but they are not listed on any page of the document. I want to print the file with table of contents, or print the table of contents separately. How can I do that?










    share|improve this question

























      2












      2








      2








      I have a pdf kinda-book file which has a table of contents as metadata in file but they are not listed on any page of the document. I want to print the file with table of contents, or print the table of contents separately. How can I do that?










      share|improve this question














      I have a pdf kinda-book file which has a table of contents as metadata in file but they are not listed on any page of the document. I want to print the file with table of contents, or print the table of contents separately. How can I do that?







      pdf






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 10 at 19:31









      CrabManCrabMan

      19418




      19418






















          1 Answer
          1






          active

          oldest

          votes


















          7














          pdftk can dump out the "bookmarks" with, e.g., pdftk file.pdf dump_data_utf8; you'll get a bunch of Bookmark* entries buried in the rest of the metadata. grep can give just them:



          $ pdftk whatever.pdf dump_data_utf8 | grep ^Bookmark
          BookmarkBegin
          BookmarkTitle: Cover
          BookmarkLevel: 1
          BookmarkPageNumber: 1
          BookmarkBegin
          BookmarkTitle: Agenda
          BookmarkLevel: 1
          BookmarkPageNumber: 2


          The "level" is the indentation level (so a level 2 is indented from a level 1). You can format that into whatever format you want for printing.



          Here is a Perl script to print it in LaTeX format, which can then be fed to e.g., pdflatex to get a PDF file (which you could even use pdftk to prepend to your original PDF). Note this is also available at https://gitlab.com/derobert/random-toys/blob/master/pdf/pdftoc-to-latex (which is a good place to send pull requests if you want to improve it):



          #!/usr/bin/perl
          use 5.024;
          use strict;
          use warnings qw(all);
          use IPC::Run3;
          use LaTeX::Encode;
          use Encode qw(decode);

          my @levels
          = qw(chapter section subsection subsubsection paragraph subparagraph);
          my @counters;

          my ($data_enc, $data);
          run3 ['pdftk', $ARGV[0], 'dump_data_utf8'], undef, $data_enc;
          $data = decode('UTF-8', $data_enc, Encode::FB_CROAK);

          my @latex_bm;

          my $bm;
          foreach (split(/n/, $data)) {
          /^Bookmark/ or next;
          if (/^BookmarkBegin$/) {
          add_latex_bm($bm) if $bm;
          $bm = {};
          } elsif (/^BookmarkLevel: (d+)$/a) {
          ++$counters[$1 - 1];
          $#counters = $1 - 1;
          $bm->{number} = join(q{.}, @counters);
          $bm->{level} = $1 - 1;
          } elsif (/^BookmarkTitle: (.+)$/) {
          $bm->{title} = latex_encode($1);
          } elsif (/^BookmarkPageNumber: (d+)$/a) {
          $bm->{page} = $1;
          } else {
          die "Unknown Bookmark tag in $_n";
          }
          }
          add_latex_bm($bm) if $bm;

          print <<LATEX;
          \documentclass{report}
          \begin{document}
          ${ join('', @latex_bm) }
          \end{document}
          LATEX

          exit 0;

          sub add_latex_bm {
          my $bm = shift;
          my $level = $levels[$bm->{level}];
          my $number = $bm->{number};
          my $title = $bm->{title};
          my $page = $bm->{page};

          push @latex_bm, <<LINE;
          \contentsline {$level}{\numberline {$number}$title}{$page}%
          LINE
          }


          Here is how to use this script:




          1. Download https://gitlab.com/derobert/random-toys/raw/master/pdf/pdftoc-to-latex?inline=false and save as pdftoc-to-latex.pl

          2. Make it executable by running chmod +x /path/to/pdftoc-to-latex.pl in the terminal

          3. Install Latex::Encode perl package. On Debian Stretch you can do so via sudo apt install liblatex-encode-perl. On other distros you will probably need to do something else.

          4. Run the script like this: /path/to/pdftoc-to-latex.pl /path/to/pdf/file.pdf > /path/to/where/you/want/tex/file.tex

          5. Compile the resulting tex file to pdf with your favorite LaTeX compiler (e.g., cd /path/to/where/you/want/tex; pdflatex file.tex)






          share|improve this answer


























          • Any you can use LaTex to take this data, and turn it into a nicely formatted, printable pdf. +1 for anyone that adds a complementary answer, on how to do this.

            – ctrl-alt-delor
            Jan 10 at 19:54











          • @ctrl-alt-delor code added

            – derobert
            Jan 10 at 20:36











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493799%2fhow-to-print-table-of-contents-of-a-pdf%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          7














          pdftk can dump out the "bookmarks" with, e.g., pdftk file.pdf dump_data_utf8; you'll get a bunch of Bookmark* entries buried in the rest of the metadata. grep can give just them:



          $ pdftk whatever.pdf dump_data_utf8 | grep ^Bookmark
          BookmarkBegin
          BookmarkTitle: Cover
          BookmarkLevel: 1
          BookmarkPageNumber: 1
          BookmarkBegin
          BookmarkTitle: Agenda
          BookmarkLevel: 1
          BookmarkPageNumber: 2


          The "level" is the indentation level (so a level 2 is indented from a level 1). You can format that into whatever format you want for printing.



          Here is a Perl script to print it in LaTeX format, which can then be fed to e.g., pdflatex to get a PDF file (which you could even use pdftk to prepend to your original PDF). Note this is also available at https://gitlab.com/derobert/random-toys/blob/master/pdf/pdftoc-to-latex (which is a good place to send pull requests if you want to improve it):



          #!/usr/bin/perl
          use 5.024;
          use strict;
          use warnings qw(all);
          use IPC::Run3;
          use LaTeX::Encode;
          use Encode qw(decode);

          my @levels
          = qw(chapter section subsection subsubsection paragraph subparagraph);
          my @counters;

          my ($data_enc, $data);
          run3 ['pdftk', $ARGV[0], 'dump_data_utf8'], undef, $data_enc;
          $data = decode('UTF-8', $data_enc, Encode::FB_CROAK);

          my @latex_bm;

          my $bm;
          foreach (split(/n/, $data)) {
          /^Bookmark/ or next;
          if (/^BookmarkBegin$/) {
          add_latex_bm($bm) if $bm;
          $bm = {};
          } elsif (/^BookmarkLevel: (d+)$/a) {
          ++$counters[$1 - 1];
          $#counters = $1 - 1;
          $bm->{number} = join(q{.}, @counters);
          $bm->{level} = $1 - 1;
          } elsif (/^BookmarkTitle: (.+)$/) {
          $bm->{title} = latex_encode($1);
          } elsif (/^BookmarkPageNumber: (d+)$/a) {
          $bm->{page} = $1;
          } else {
          die "Unknown Bookmark tag in $_n";
          }
          }
          add_latex_bm($bm) if $bm;

          print <<LATEX;
          \documentclass{report}
          \begin{document}
          ${ join('', @latex_bm) }
          \end{document}
          LATEX

          exit 0;

          sub add_latex_bm {
          my $bm = shift;
          my $level = $levels[$bm->{level}];
          my $number = $bm->{number};
          my $title = $bm->{title};
          my $page = $bm->{page};

          push @latex_bm, <<LINE;
          \contentsline {$level}{\numberline {$number}$title}{$page}%
          LINE
          }


          Here is how to use this script:




          1. Download https://gitlab.com/derobert/random-toys/raw/master/pdf/pdftoc-to-latex?inline=false and save as pdftoc-to-latex.pl

          2. Make it executable by running chmod +x /path/to/pdftoc-to-latex.pl in the terminal

          3. Install Latex::Encode perl package. On Debian Stretch you can do so via sudo apt install liblatex-encode-perl. On other distros you will probably need to do something else.

          4. Run the script like this: /path/to/pdftoc-to-latex.pl /path/to/pdf/file.pdf > /path/to/where/you/want/tex/file.tex

          5. Compile the resulting tex file to pdf with your favorite LaTeX compiler (e.g., cd /path/to/where/you/want/tex; pdflatex file.tex)






          share|improve this answer


























          • Any you can use LaTex to take this data, and turn it into a nicely formatted, printable pdf. +1 for anyone that adds a complementary answer, on how to do this.

            – ctrl-alt-delor
            Jan 10 at 19:54











          • @ctrl-alt-delor code added

            – derobert
            Jan 10 at 20:36
















          7














          pdftk can dump out the "bookmarks" with, e.g., pdftk file.pdf dump_data_utf8; you'll get a bunch of Bookmark* entries buried in the rest of the metadata. grep can give just them:



          $ pdftk whatever.pdf dump_data_utf8 | grep ^Bookmark
          BookmarkBegin
          BookmarkTitle: Cover
          BookmarkLevel: 1
          BookmarkPageNumber: 1
          BookmarkBegin
          BookmarkTitle: Agenda
          BookmarkLevel: 1
          BookmarkPageNumber: 2


          The "level" is the indentation level (so a level 2 is indented from a level 1). You can format that into whatever format you want for printing.



          Here is a Perl script to print it in LaTeX format, which can then be fed to e.g., pdflatex to get a PDF file (which you could even use pdftk to prepend to your original PDF). Note this is also available at https://gitlab.com/derobert/random-toys/blob/master/pdf/pdftoc-to-latex (which is a good place to send pull requests if you want to improve it):



          #!/usr/bin/perl
          use 5.024;
          use strict;
          use warnings qw(all);
          use IPC::Run3;
          use LaTeX::Encode;
          use Encode qw(decode);

          my @levels
          = qw(chapter section subsection subsubsection paragraph subparagraph);
          my @counters;

          my ($data_enc, $data);
          run3 ['pdftk', $ARGV[0], 'dump_data_utf8'], undef, $data_enc;
          $data = decode('UTF-8', $data_enc, Encode::FB_CROAK);

          my @latex_bm;

          my $bm;
          foreach (split(/n/, $data)) {
          /^Bookmark/ or next;
          if (/^BookmarkBegin$/) {
          add_latex_bm($bm) if $bm;
          $bm = {};
          } elsif (/^BookmarkLevel: (d+)$/a) {
          ++$counters[$1 - 1];
          $#counters = $1 - 1;
          $bm->{number} = join(q{.}, @counters);
          $bm->{level} = $1 - 1;
          } elsif (/^BookmarkTitle: (.+)$/) {
          $bm->{title} = latex_encode($1);
          } elsif (/^BookmarkPageNumber: (d+)$/a) {
          $bm->{page} = $1;
          } else {
          die "Unknown Bookmark tag in $_n";
          }
          }
          add_latex_bm($bm) if $bm;

          print <<LATEX;
          \documentclass{report}
          \begin{document}
          ${ join('', @latex_bm) }
          \end{document}
          LATEX

          exit 0;

          sub add_latex_bm {
          my $bm = shift;
          my $level = $levels[$bm->{level}];
          my $number = $bm->{number};
          my $title = $bm->{title};
          my $page = $bm->{page};

          push @latex_bm, <<LINE;
          \contentsline {$level}{\numberline {$number}$title}{$page}%
          LINE
          }


          Here is how to use this script:




          1. Download https://gitlab.com/derobert/random-toys/raw/master/pdf/pdftoc-to-latex?inline=false and save as pdftoc-to-latex.pl

          2. Make it executable by running chmod +x /path/to/pdftoc-to-latex.pl in the terminal

          3. Install Latex::Encode perl package. On Debian Stretch you can do so via sudo apt install liblatex-encode-perl. On other distros you will probably need to do something else.

          4. Run the script like this: /path/to/pdftoc-to-latex.pl /path/to/pdf/file.pdf > /path/to/where/you/want/tex/file.tex

          5. Compile the resulting tex file to pdf with your favorite LaTeX compiler (e.g., cd /path/to/where/you/want/tex; pdflatex file.tex)






          share|improve this answer


























          • Any you can use LaTex to take this data, and turn it into a nicely formatted, printable pdf. +1 for anyone that adds a complementary answer, on how to do this.

            – ctrl-alt-delor
            Jan 10 at 19:54











          • @ctrl-alt-delor code added

            – derobert
            Jan 10 at 20:36














          7












          7








          7







          pdftk can dump out the "bookmarks" with, e.g., pdftk file.pdf dump_data_utf8; you'll get a bunch of Bookmark* entries buried in the rest of the metadata. grep can give just them:



          $ pdftk whatever.pdf dump_data_utf8 | grep ^Bookmark
          BookmarkBegin
          BookmarkTitle: Cover
          BookmarkLevel: 1
          BookmarkPageNumber: 1
          BookmarkBegin
          BookmarkTitle: Agenda
          BookmarkLevel: 1
          BookmarkPageNumber: 2


          The "level" is the indentation level (so a level 2 is indented from a level 1). You can format that into whatever format you want for printing.



          Here is a Perl script to print it in LaTeX format, which can then be fed to e.g., pdflatex to get a PDF file (which you could even use pdftk to prepend to your original PDF). Note this is also available at https://gitlab.com/derobert/random-toys/blob/master/pdf/pdftoc-to-latex (which is a good place to send pull requests if you want to improve it):



          #!/usr/bin/perl
          use 5.024;
          use strict;
          use warnings qw(all);
          use IPC::Run3;
          use LaTeX::Encode;
          use Encode qw(decode);

          my @levels
          = qw(chapter section subsection subsubsection paragraph subparagraph);
          my @counters;

          my ($data_enc, $data);
          run3 ['pdftk', $ARGV[0], 'dump_data_utf8'], undef, $data_enc;
          $data = decode('UTF-8', $data_enc, Encode::FB_CROAK);

          my @latex_bm;

          my $bm;
          foreach (split(/n/, $data)) {
          /^Bookmark/ or next;
          if (/^BookmarkBegin$/) {
          add_latex_bm($bm) if $bm;
          $bm = {};
          } elsif (/^BookmarkLevel: (d+)$/a) {
          ++$counters[$1 - 1];
          $#counters = $1 - 1;
          $bm->{number} = join(q{.}, @counters);
          $bm->{level} = $1 - 1;
          } elsif (/^BookmarkTitle: (.+)$/) {
          $bm->{title} = latex_encode($1);
          } elsif (/^BookmarkPageNumber: (d+)$/a) {
          $bm->{page} = $1;
          } else {
          die "Unknown Bookmark tag in $_n";
          }
          }
          add_latex_bm($bm) if $bm;

          print <<LATEX;
          \documentclass{report}
          \begin{document}
          ${ join('', @latex_bm) }
          \end{document}
          LATEX

          exit 0;

          sub add_latex_bm {
          my $bm = shift;
          my $level = $levels[$bm->{level}];
          my $number = $bm->{number};
          my $title = $bm->{title};
          my $page = $bm->{page};

          push @latex_bm, <<LINE;
          \contentsline {$level}{\numberline {$number}$title}{$page}%
          LINE
          }


          Here is how to use this script:




          1. Download https://gitlab.com/derobert/random-toys/raw/master/pdf/pdftoc-to-latex?inline=false and save as pdftoc-to-latex.pl

          2. Make it executable by running chmod +x /path/to/pdftoc-to-latex.pl in the terminal

          3. Install Latex::Encode perl package. On Debian Stretch you can do so via sudo apt install liblatex-encode-perl. On other distros you will probably need to do something else.

          4. Run the script like this: /path/to/pdftoc-to-latex.pl /path/to/pdf/file.pdf > /path/to/where/you/want/tex/file.tex

          5. Compile the resulting tex file to pdf with your favorite LaTeX compiler (e.g., cd /path/to/where/you/want/tex; pdflatex file.tex)






          share|improve this answer















          pdftk can dump out the "bookmarks" with, e.g., pdftk file.pdf dump_data_utf8; you'll get a bunch of Bookmark* entries buried in the rest of the metadata. grep can give just them:



          $ pdftk whatever.pdf dump_data_utf8 | grep ^Bookmark
          BookmarkBegin
          BookmarkTitle: Cover
          BookmarkLevel: 1
          BookmarkPageNumber: 1
          BookmarkBegin
          BookmarkTitle: Agenda
          BookmarkLevel: 1
          BookmarkPageNumber: 2


          The "level" is the indentation level (so a level 2 is indented from a level 1). You can format that into whatever format you want for printing.



          Here is a Perl script to print it in LaTeX format, which can then be fed to e.g., pdflatex to get a PDF file (which you could even use pdftk to prepend to your original PDF). Note this is also available at https://gitlab.com/derobert/random-toys/blob/master/pdf/pdftoc-to-latex (which is a good place to send pull requests if you want to improve it):



          #!/usr/bin/perl
          use 5.024;
          use strict;
          use warnings qw(all);
          use IPC::Run3;
          use LaTeX::Encode;
          use Encode qw(decode);

          my @levels
          = qw(chapter section subsection subsubsection paragraph subparagraph);
          my @counters;

          my ($data_enc, $data);
          run3 ['pdftk', $ARGV[0], 'dump_data_utf8'], undef, $data_enc;
          $data = decode('UTF-8', $data_enc, Encode::FB_CROAK);

          my @latex_bm;

          my $bm;
          foreach (split(/n/, $data)) {
          /^Bookmark/ or next;
          if (/^BookmarkBegin$/) {
          add_latex_bm($bm) if $bm;
          $bm = {};
          } elsif (/^BookmarkLevel: (d+)$/a) {
          ++$counters[$1 - 1];
          $#counters = $1 - 1;
          $bm->{number} = join(q{.}, @counters);
          $bm->{level} = $1 - 1;
          } elsif (/^BookmarkTitle: (.+)$/) {
          $bm->{title} = latex_encode($1);
          } elsif (/^BookmarkPageNumber: (d+)$/a) {
          $bm->{page} = $1;
          } else {
          die "Unknown Bookmark tag in $_n";
          }
          }
          add_latex_bm($bm) if $bm;

          print <<LATEX;
          \documentclass{report}
          \begin{document}
          ${ join('', @latex_bm) }
          \end{document}
          LATEX

          exit 0;

          sub add_latex_bm {
          my $bm = shift;
          my $level = $levels[$bm->{level}];
          my $number = $bm->{number};
          my $title = $bm->{title};
          my $page = $bm->{page};

          push @latex_bm, <<LINE;
          \contentsline {$level}{\numberline {$number}$title}{$page}%
          LINE
          }


          Here is how to use this script:




          1. Download https://gitlab.com/derobert/random-toys/raw/master/pdf/pdftoc-to-latex?inline=false and save as pdftoc-to-latex.pl

          2. Make it executable by running chmod +x /path/to/pdftoc-to-latex.pl in the terminal

          3. Install Latex::Encode perl package. On Debian Stretch you can do so via sudo apt install liblatex-encode-perl. On other distros you will probably need to do something else.

          4. Run the script like this: /path/to/pdftoc-to-latex.pl /path/to/pdf/file.pdf > /path/to/where/you/want/tex/file.tex

          5. Compile the resulting tex file to pdf with your favorite LaTeX compiler (e.g., cd /path/to/where/you/want/tex; pdflatex file.tex)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 11 at 14:19

























          answered Jan 10 at 19:43









          derobertderobert

          73.1k8154211




          73.1k8154211













          • Any you can use LaTex to take this data, and turn it into a nicely formatted, printable pdf. +1 for anyone that adds a complementary answer, on how to do this.

            – ctrl-alt-delor
            Jan 10 at 19:54











          • @ctrl-alt-delor code added

            – derobert
            Jan 10 at 20:36



















          • Any you can use LaTex to take this data, and turn it into a nicely formatted, printable pdf. +1 for anyone that adds a complementary answer, on how to do this.

            – ctrl-alt-delor
            Jan 10 at 19:54











          • @ctrl-alt-delor code added

            – derobert
            Jan 10 at 20:36

















          Any you can use LaTex to take this data, and turn it into a nicely formatted, printable pdf. +1 for anyone that adds a complementary answer, on how to do this.

          – ctrl-alt-delor
          Jan 10 at 19:54





          Any you can use LaTex to take this data, and turn it into a nicely formatted, printable pdf. +1 for anyone that adds a complementary answer, on how to do this.

          – ctrl-alt-delor
          Jan 10 at 19:54













          @ctrl-alt-delor code added

          – derobert
          Jan 10 at 20:36





          @ctrl-alt-delor code added

          – derobert
          Jan 10 at 20:36


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493799%2fhow-to-print-table-of-contents-of-a-pdf%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How do I know what Microsoft account the skydrive app is syncing to?

          When does type information flow backwards in C++?

          Grease: Live!