美文网首页
html、xhtml转pdf工具princexml试用

html、xhtml转pdf工具princexml试用

作者: 千羽之城88 | 来源:发表于2023-10-23 12:21 被阅读0次

这个工具认识很多年了,之前用来把某一个中文博客转成pdf在kindle上看。最开始只支持windows平台,现在已经支持macos等其他平台:

下载:https://www.princexml.com/

一、基本用法

# 转换所有xhtml为pdf
prince --page-size=a6 *.xhtml -o all.pdf

二、添加页码和页眉

@page {
    @bottom-center {
        content: counter(page); #  页码
        font-size: 8pt; # 页码字号
    }
    @top-center {
        content: element(header) ; # 页眉
        font-size: 8pt;  # 页眉字号
    }
}

h2 > * { 
    position: running(header); # 页眉内容
}

三、标题计数器和内容分离

原始的xhtml:

<h2><span class="counter">第一章</span>测试</h2>

修改为:

<h2><span class="counter">第一章</span><span>测试</span></h2>

上面的内容可解析为:firstchild和lastchild,即实现了计数器和内容的分离。

@page {
    @bottom-center {
        content: counter(page);
        font-size: 8pt;
    }
    @top-left {
       font-size: 8pt;
       font-family: sans-serif;
       content: string(term, first);
    }
    @top-right {
       font-size: 8pt;
       font-family: sans-serif;
       content: string(chapter-title);        
    }
}

h2 :first-child { 
    string-set: term content() ;
}

h2 :last-child {
    string-set: chapter-title content();
}

四、设置页面

 prince --page-margin=10mm --page-size=a6

命令行帮助:

Usage:
  prince [OPTIONS] doc.html              Convert doc.html to doc.pdf
  prince [OPTIONS] doc.html -o out.pdf   Convert doc.html to out.pdf
  prince [OPTIONS] FILES... -o out.pdf   Combine multiple files to out.pdf

General Options:
  -h, --help                  Display usage and options.
  --version                   Display version information.
  --credits                   Display credits and acknowledgements.

Logging Options:
  -v, --verbose               Log informative messages.
  --debug                     Log debug messages.
  --log=FILE                  Append log messages to a file.
  --no-warn-css               Do not warn about CSS (implies the following).
  --no-warn-css-unknown       Do not warn about unknown CSS features.
  --no-warn-css-unsupported   Do not warn about unsupported CSS features.
  --structured-log=TYPE       Enable structured logging on stderr.
                              [normal|quiet|progress|buffered]

Input Options:
  -i, --input=FORMAT          Set input format. [xml|html|auto]
  -l, --input-list=FILE       Read input file list from specified file.
  --baseurl=URL               Specify the base URL of the input document.
  --remap=URL=DIR             Map URL prefix to a local directory.
  --fileroot=DIR              Specify the root directory for absolute filenames
                              (deprecated).
  --iframes                   Enable HTML iframes.
  --xinclude                  Enable XInclude processing and --xml-external-entities.
  --xml-external-entities     Enable XML external entities (XXE) and external DTDs.
  --no-local-files            Disable access to local files.

Network Options:
  --no-network                Disable network access (prevents HTTP downloads).
  --auth-user=USER            Specify username for HTTP authentication.
  --auth-password=PASS        Specify password for HTTP authentication.
  --auth-server=SERVER        Only send USER:PASS to this server.
  --auth-scheme=SCHEME        Only send USER:PASS for this scheme. [http|https]
  --auth-method=METHODS       Specify HTTP authentication methods.
                              [basic,digest,ntlm,negotiate]
  --auth=URL                  Specify URL with user:pass@server for
                              authentication.
  --no-auth-preemptive        Do not authenticate with named servers until
                              asked.
  --http-proxy=PROXY          Specify the HTTP proxy server.
  --http-timeout=SEC          Specify the HTTP timeout in seconds.
  --cookie=COOKIE             Specify a Set-Cookie header value.
  --cookie-file=FILE          Specify a file containing HTTP cookies.
  --ssl-cacert=FILE           Specify an SSL CA certificate file.
  --ssl-capath=PATH           Specify an SSL CA certificate directory.
  --ssl-cert=FILE             Specify an SSL client certificate file.
  --ssl-cert-type=TYPE        Specify the SSL client certificate file type. [pem,der]
  --ssl-key=FILE              Specify an SSL private key file.
  --ssl-key-type=TYPE         Specify the SSL private key file type. [pem,der]
  --ssl-key-password=PASS     Specify passphrase for the private key.
  --ssl-version=VERSION       Specify an SSL/TLS version to use.
  --insecure                  Disable SSL verification (not recommended).
  --ssl-no-revoke             Disable certificate revocation checks (Windows).
  --ssl-revoke-best-effort    Ignore missing/offline CRL distribution points (Windows).
  --no-redirects              Do not follow HTTP/HTTPS redirects.
  --no-parallel-downloads     Disable parallel downloads.

JavaScript Options:
  -j, --javascript            Enable JavaScript in HTML documents.
  --script=FILE               Run an external script.
  --max-passes=NUM            Maximum number of times that JavaScript may
                              modify the document and repeat layout.

CSS Options:
  -s, --style=FILE            Apply an external style sheet.
  --media=MEDIA               Specify the media type (eg. print, screen).
  --page-size=SIZE            Specify the page size (eg. A4).
  --page-margin=MARGIN        Specify the page margin (eg. 20mm).
  --no-author-style           Ignore author style sheets.
  --no-default-style          Ignore default style sheets.

PDF Output Options:
  -o, --output=FILE           Specify the output PDF filename.
  --pdf-profile=PROFILE       Specify the PDF profile to use.
  --pdf-output-intent=ICC     Specify the output intent ICC profile.
  --pdf-script=FILE           Include an AcroJS script to run when the PDF is opened.
  --pdf-event-script=EVENT:FILE Include an AcroJS script to run on a specific event.
                              [will-close, will-save, did-save, will-print, did-print]
  --attach=FILE               Attach a file to the PDF.
  --tagged-pdf                Produce tagged PDF.
  --pdf-forms                 Enable generation of interactive form fields by default.
  --no-artificial-fonts       Disable synthesis of missing bold/italic fonts.
  --no-embed-fonts            Disable font embedding in PDF output.
  --no-subset-fonts           Disable font subsetting in PDF output.
  --force-identity-encoding   Use identity encoding for all fonts.
  --no-compress               Disable compression of PDF output.
  --no-object-streams         Disable use of PDF object streams.
  --convert-colors            Convert colors to output intent color space.
  --fallback-cmyk-profile=ICC Set fallback ICC profile for uncalibrated CMYK.

PDF Metadata Options:
  --pdf-lang=LANG             Specify the primary language.
  --pdf-xmp-metadata          Include HTML metadata as XMP in the PDF.
  --pdf-xmp=FILE              Include additional XMP metadata in the PDF.
  --pdf-title=TITLE           Set PDF document title.
  --pdf-subject=SUBJECT       Set PDF document subject.
  --pdf-author=AUTHOR         Set PDF document author.
  --pdf-keywords=KEYWORDS     Set PDF document keywords.
  --pdf-creator=CREATOR       Set PDF document creator.

PDF Encryption Options:
  --encrypt                   Encrypt PDF output.
  --key-bits=NUM              Set encryption key size [40|128].
  --user-password=PASS        Set PDF user password.
  --owner-password=PASS       Set PDF owner password.
  --disallow-print            Disallow printing of PDF output.
  --disallow-modify           Disallow modification of PDF output.
  --disallow-copy             Disallow copying from PDF output.
  --disallow-annotate         Disallow annotation of PDF output.
  --allow-copy-for-accessibility Allow copying content for accessibility.
  --allow-assembly            Allow document assembly.

Raster Output Options:
  --raster-output=TEMPLATE    Enable raster output.
  --raster-format=FORMAT      Set raster output format. [auto|png|jpeg]
  --raster-jpeg-quality=NUM   Set quality for raster JPEG output.
  --raster-pages=PAGE         Set range of pages to rasterise. [all|first|NUM]
  --raster-dpi=DPI            Set resolution of raster output.
  --raster-background=COLOR   Set background color. [white|transparent]
  --raster-threads=NUM        Set number of threads for raster output.

Fail-Safe Options:
  --fail-safe                 Set all of the following fail-safe options.
  --fail-dropped-content      Fail if any content is dropped.
  --fail-missing-resources    Fail if any resources cannot be loaded.
  --fail-missing-glyphs       Fail if glyphs cannot be found for any characters.
  --fail-stripped-transparency Fail if any transparency is stripped.
  --fail-pdf-profile-error    Fail if unable to comply with the PDF profile.
  --fail-pdf-tag-error        Fail if there are problems tagging the PDF.

Utility Options:
  --scanfonts FILES...        Scan font files and create a CSS file.
  --control                   Communicate via the control interface.

For more information, visit http://www.princexml.com
命令 参数 输出 解释
prince [OPTIONS] doc.html Convert doc.html to doc.pdf
prince [OPTIONS] doc.html -o out.pdf Convert doc.html to out.pdf
prince [OPTIONS] FILES... -o out.pdf Combine multiple files to out.pdf

参考

  1. https://www.princexml.com/doc/paged/

相关文章

网友评论

      本文标题:html、xhtml转pdf工具princexml试用

      本文链接:https://www.haomeiwen.com/subject/sztjidtx.html