Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 通过命令行将书签创建到PDF文件中_R_Macos_Pdf_Pdf Generation - Fatal编程技术网

R 通过命令行将书签创建到PDF文件中

R 通过命令行将书签创建到PDF文件中,r,macos,pdf,pdf-generation,R,Macos,Pdf,Pdf Generation,我正在搜索一个命令行工具,用于将书签添加到PDF文件中 我有一个页码和一个标签。希望创建名为标签的书签链接到页码 有人知道这样做的命令行工具(最好是OSX)吗 我有大约4000页的PDF文件和大约150个书签,我很想把它自动化 我的计划是在r脚本中使用系统调用 编辑 我创建了大约4000个带有图形的PDF文件,并使用OSX系统命令/system/Library/Automator/combinepdf Pages.action/Contents/Resources/join.py将PDF文件连接

我正在搜索一个命令行工具,用于将书签添加到PDF文件中

我有一个
页码
和一个
标签
。希望创建名为
标签的书签
链接到
页码

有人知道这样做的命令行工具(最好是OSX)吗

我有大约4000页的PDF文件和大约150个书签,我很想把它自动化

我的计划是在r脚本中使用系统调用

编辑


我创建了大约4000个带有图形的PDF文件,并使用OSX系统命令
/system/Library/Automator/combinepdf Pages.action/Contents/Resources/join.py
将PDF文件连接在一起。以前我使用的是
pdfjam
软件包中的
pdfjoin
,但速度太慢了。最后,我通过Adobe Acrobat Professional手动添加书签的方式获得了我的PDF。

好的,这里有一个快速、肮脏的方法可以同时完成三项工作:

  • 合并400个单页PDF
  • 创建文档顶级目录(目录)
  • 为每个页面创建PDF书签
  • 它涉及到使用LaTeX安装

    您可以从一个空的LaTeX模板开始,如下所示:

    \documentclass[]{article}
    \usepackage{pdfpages}
    \usepackage{hyperref}
        \hypersetup{breaklinks=true,
                    bookmarks=true,
                    pdfauthor={},
                    pdftitle={},
                    colorlinks=true,
                    citecolor=blue,
                    urlcolor=blue,
                    linkcolor=magenta,
                    pdfborder={0 0 0}}
    \begin{document}
    
    {
        \hypersetup{linkcolor=black}
        \setcounter{tocdepth}{3}
        % Comment next line in or out if you want a ToC or not:
        \tableofcontents
    }
    
    %% Here goes your additional code:
    %% 1 line per included PDF!
    
    \end{document}
    
    现在,就在该模板的最后一行之前,您可以为每个要包含的外部PDF文件插入一行

  • 如果您想生成ToC,必须将其格式化为:

    \includepdf[pages={<pagenumber>},addtotoc{<pagenumber>,<section>,<level>,\
                       <heading>,<label>}]{pdffilename.pdf}
    
    [/Page 1   /View [/XYZ null null null] /Title (This is page 1)         /OUT pdfmark
    [/Page 2   /View [/XYZ null null null] /Title (Dunno which page this is....) /OUT pdfmark
    [/Page 3   /View [/XYZ null null null] /Title (Some other name)        /OUT pdfmark
    [/Page 4   /View [/XYZ null null null] /Title (File 4)                 /OUT pdfmark
    [/Page 5   /View [/XYZ null null null] /Title (File 5)                 /OUT pdfmark
    [/Page 6   /View [/XYZ null null null] /Title (File 6)                 /OUT pdfmark
    [/Page 7   /View [/XYZ null null null] /Title (File 7)                 /OUT pdfmark
    % more lines for more pages to bookmark...
    [/Page 13  /View [/XYZ null null null] /Title (File 13)                /OUT pdfmark
    [/Page 14  /View [/XYZ null null null] /Title (Bookmark for page 14)   /OUT pdfmark
    % more lines for more pages to bookmark...
    
    使用这些测试文件,我可以使插入模板的行如下所示:

    \includepdf[addtotoc={1,section,1,Page 1 (First),p1}]{p1.pdf}
    \includepdf[addtotoc={1,section,1,Page 2,p2}]{p2.pdf}
    \includepdf[addtotoc={1,section,1,Page 3,p3}]{p3.pdf}
    [...]
    \includepdf[addtotoc={1,section,1,Page 11 (In the Middle),p11}]{p11.pdf}
    [...]
    \includepdf[addtotoc={1,section,1,Page 20 (Last),p20}]{p20.pdf}
    
    使用插入的行保存模板,然后运行以下命令两次:

     pdflatex template.tex
     pdflatex template.tex
    
    生成的文件将包含书签,如Preview.app中所示:


    注意:乳胶可通过两种方法用于OSX:


    如果我有更多的时间,我会在以后或几天内,在命令行中添加一两个其他方法来插入书签

    现在这个必须要做,因为我从来没有在这里展示过,所以,AFAICR

    但我想,因为你给出了背景“我正在合并一页PDF,速度很慢;现在我也想添加书签……”,我可以用一种方法演示如何做到这一点


    提示:其他方法之一是使用适用于Mac OS X的
    pdftk

    这里是另一个答案。这一个使用Ghostscript将PDF处理为PDF,并使用PostScript操作符插入书签

    有关pdfmark主题的介绍,请参见:

    • 托马斯·默兹的
    此方法包括两个步骤:

  • 创建一个文本文件(实际上是一个PostScript文件),其中包含一组有限的
    pdfmark
    命令,每行一个命令和要添加的书签
  • 运行Ghostscript命令,将当前PDF文件与文本文件一起处理
  • 1. 文本文件上的内容应如下所示:

    \includepdf[pages={<pagenumber>},addtotoc{<pagenumber>,<section>,<level>,\
                       <heading>,<label>}]{pdffilename.pdf}
    
    [/Page 1   /View [/XYZ null null null] /Title (This is page 1)         /OUT pdfmark
    [/Page 2   /View [/XYZ null null null] /Title (Dunno which page this is....) /OUT pdfmark
    [/Page 3   /View [/XYZ null null null] /Title (Some other name)        /OUT pdfmark
    [/Page 4   /View [/XYZ null null null] /Title (File 4)                 /OUT pdfmark
    [/Page 5   /View [/XYZ null null null] /Title (File 5)                 /OUT pdfmark
    [/Page 6   /View [/XYZ null null null] /Title (File 6)                 /OUT pdfmark
    [/Page 7   /View [/XYZ null null null] /Title (File 7)                 /OUT pdfmark
    % more lines for more pages to bookmark...
    [/Page 13  /View [/XYZ null null null] /Title (File 13)                /OUT pdfmark
    [/Page 14  /View [/XYZ null null null] /Title (Bookmark for page 14)   /OUT pdfmark
    % more lines for more pages to bookmark...
    
    例如,将此文件命名为:
    addmybookmarks.txt

    2. 现在运行以下命令:

    gs -o bookmarked.pdf   \
       -sDEVICE=pdfwrite   \
        addmybookmarks.txt \
       -f original.pdf
    
    生成的PDF,
    bookmarked.PDF
    现在包含书签。请参见此屏幕截图:


    pdfmark“>

    您也可以使用
    pdftk
    。它也可以使用

    我不会在这里和现在讨论所有细节,因为其他地方已经做了大量的工作。只是简单地说:

  • 从原始文件(不带书签)创建示例PDF
  • 使用Adobe Acrobat添加一些书签(您似乎可以访问这些书签)
  • 运行以下命令之一:

    pdftk my.pdf dump_data output -
    pdftk my.pdf dump_data output bookmarks+otherdata.txt
    
  • 研究输出的格式

  • 通过添加所需的所有条目来修改output.txt文件
  • 再次运行PDFTK:

    pdftk my.pdf update_info bookmarks.txt output bookmarked.pdf
    
  • 其他信息

    这是我在上面第4步检查后注意到的书签格式

    BookmarkBegin
    BookmarkTitle: -- Your Title 1 --
    BookmarkLevel: 1
    BookmarkPageNumber: 1
    BookmarkBegin
    BookmarkTitle: -- Your Title 2 --
    BookmarkLevel: 1
    BookmarkPageNumber: 2
    BookmarkBegin
    BookmarkTitle: -- Your Title 3 --
    ...
    ...
    and so on...
    

    并在适当的位置替换上述内容。

    以下是将书签添加到目录的python方法。在MacOS上运行,无需任何其他安装

    #!/usr/bin/python    
    from Foundation import  NSURL, NSString
    import Quartz as Quartz
    import sys
    
    # You will need to change these filepaths to a local test pdf and an output file.
    infile = "/path/to/file.pdf"
    outfile = "/path/to/output.pdf"
    
    def getOutline(page, label):
        # Create Destination
        myPage = myPDF.pageAtIndex_(page)
        pageSize = myPage.boundsForBox_(Quartz.kCGPDFMediaBox)
        x = 0
        y = Quartz.CGRectGetMaxY(pageSize)
        pagePoint = Quartz.CGPointMake(x,y)
        myDestination = Quartz.PDFDestination.alloc().initWithPage_atPoint_(myPage, pagePoint)
        myLabel = NSString.stringWithString_(label)
        myOutline = Quartz.PDFOutline.alloc().init()
        myOutline.setLabel_(myLabel)
        myOutline.setDestination_(myDestination)
        return myOutline
    
    pdfURL = NSURL.fileURLWithPath_(infile)
    myPDF = Quartz.PDFDocument.alloc().initWithURL_(pdfURL)
    if myPDF:
        # Here's where you list your page index (starts at 0) and label.
        outline1 = getOutline(0, 'Page 1')
        outline2 = getOutline(1, 'Page 2')
        outline3 = getOutline(2, 'Page 3')
    
        # Create a root Outline and add each outline. (Needs a loop.)
        rootOutline = Quartz.PDFOutline.alloc().init()
        rootOutline.insertChild_atIndex_(outline1, 0)
        rootOutline.insertChild_atIndex_(outline2, 1)
        rootOutline.insertChild_atIndex_(outline3, 2)
        myPDF.setOutlineRoot_(rootOutline)
        myPDF.writeToFile_(outfile)
    

    我不确定你是否有生成PDF的代码,或者只生成PDF文件本身。如果是前者,我们需要更多详细信息。谢谢@Roland,我添加了tome信息。可以更新/插入书签。此外,这里有一个关于pdftk的潜在有用线程仅适用于Windows,因此它不适合我的需要。无论如何,谢谢”pdftk仅适用于Windows…”不正确!请参阅我的答案。它包含一个直接下载OSX.pkg安装程序的链接(从原始的
    pdftk
    -供应商处,而不是从某个垃圾第三方提供商处)…实际上,我非常喜欢这个解决方案,非常清晰的语法和非常容易编写的脚本。。。Thanks@drmariod:实际上,我最不喜欢这个解决方案:)我最喜欢这个解决方案,也是我能理解的最简单的解决方案..因为我已经有了pdftk。我没有运行
    addtooc
    命令…它说找不到该命令,但可以找到
    pdfpages
    包…所以我不明白这里的问题:-(@dmariod:没有看到你的代码,我说不出它有什么问题。可能是s.th.非常简单,写代码时盯着行的眼睛再也认不出来了,但是“第三方”“眼睛很容易……也发生在我身上,而且不止一次:)我不敢相信这会得不到选票