国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

使用WeasyPrint將HTML轉(zhuǎn)換為Python PDF生成

雖然使用Python處理 PDF 文檔的方法有很多種,但我發(fā)現(xiàn)生成或編輯 HTML 比嘗試弄清楚 PDF 格式的復(fù)雜性更容易、更可靠。當(dāng)然,有令人尊敬的ReportLab,如果您不喜歡 HTML,我鼓勵您研究該選項。還有PyPDF2?;蛘咭苍S是PyPDF3?不,也許是PyPDF4!嗯...看到問題了嗎?我最好的猜測是 PyPDF3,無論它的價值如何。

使用WeasyPrint將HTML轉(zhuǎn)換為Python PDF生成

這么多選擇...

但如果您熟悉 HTML,那么有一個簡單的選擇。

輸入WeasyPrint。它需要 HTML 和 CSS,并將其轉(zhuǎn)換為可用且可能美觀的 PDF 文檔。

可以在關(guān)聯(lián)的 Github 存儲庫中訪問本文中的代碼示例。隨意克隆和適應(yīng)。

github.com/bowmanjd/pyweasyprintdemo

安裝

要安裝WeasyPrint,我建議您首先使用您選擇的工具設(shè)置一個虛擬環(huán)境。

然后,安裝就像在激活的虛擬環(huán)境中執(zhí)行類似以下操作一樣簡單:

pip install weasyprin

上述方案的替代方案,具體取決于您的工具:

poetry add weasyprint
conda install -c conda-forge weasyprint
pipenv install weasyprint

你明白了。

如果您只需要weasyprint命令行工具,您甚至可以使用 pipx并使用pipx install weasyprint. 雖然這不會使作為 Python 庫的訪問變得非常方便,但如果您只想將網(wǎng)頁轉(zhuǎn)換為 PDF,這可能就是您所需要的。

命令行工具(Python 使用可選)

安裝后,weasyprint命令行工具即可使用。您可以將 HTML 文件或網(wǎng)頁轉(zhuǎn)換為 PDF。例如,您可以嘗試以下操作:

weasyprint \
"https://en.測試網(wǎng)址.org/wiki/Python_(programming_language)" \
python.pdf

python.pdf上面的命令將在當(dāng)前工作目錄中保存一個文件,該文件是從百科上的 Python 編程語言英文文章的HTML 轉(zhuǎn)換而來的。它并不完美,但希望它能給你一個想法。

當(dāng)然,您不必指定網(wǎng)址。本地 HTML 文件工作正常,并且它們提供對內(nèi)容和樣式的必要控制。

weasyprint sample.html out/sample.pdf

請隨意下載sample.html與本文內(nèi)容相關(guān)的sample.css樣式表。

CSS

body {
  font-family: sans-serif;
}
code {
  font-family: monospace;
  background: #ccc;
  padding: 2px;
}
pre code {
  display: block;
}
img {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 90%;
}
@media print {
  a::after {
    content: " (" attr(href) ") ";
  }
  pre {
    white-space: pre-wrap;
  }
  @page {
    margin: 0.75in;
    size: Letter;
    @top-right {
      content: counter(page);
    }
  }
  @page :first {
    @top-right {
      content: "";
    }
  }
}

HTML

<!DOCTYPE html>
<html>
  <head>
    <title>PDF Generation with Python and WeasyPrint</title>
    <link href="sample.css" rel="stylesheet" />
  </head>
  <body>
    <img
      src="https://dev-to-uploads.s3.amazonaws.com/i/03go0ipro79sbt8ir7oq.png"
      alt="Python and PDF"
    />
    <h1>Python PDF Generation from HTML with WeasyPrint</h1>
    <p>
      While there are numerous ways to handle PDF documents with
      <a href="https://python.org">Python</a>, I find generating or editing HTML
      far easier and more reliable than trying to figure out the intricacies of
      the PDF format. Sure, there is the venerable
      <a href="https://pypi.org/project/reportlab/">ReportLab</a>, and if HTML
      is not your cup of tea, I encourage you to look into that option. There is
      also <a href="https://mstamy2.github.io/PyPDF2/">PyPDF2</a>. Or maybe
      <a href="https://github.com/sfneal/PyPDF3">PyPDF3</a>? No, perhaps
      <a href="https://github.com/claird/PyPDF4">PyPDF4</a>! Hmmm... see the
      problem? My best guess is PyPDF3, for what that is worth.
    </p>
    <p>So many choices...</p>
    <p>
      <img
        src="https://dev-to-uploads.s3.amazonaws.com/i/omcprzuh7n6u0nyzshqv.png"
        alt="So many choices in the cereal aisle"
      />
    </p>
    <p>But there is an easy choice if you are comfortable with HTML.</p>
    <p>
      Enter <a href="https://weasyprint.org/">WeasyPrint</a>. It takes HTML and
      CSS, and converts it to a usable and potentially beautiful PDF document.
    </p>
    <blockquote>
      <p>
        The code samples in this article can be accessed in
        <a href="https://github.com/bowmanjd/pyweasyprintdemo"
          >the associated Github repo</a
        >. Feel free to clone and adapt.
      </p>
    </blockquote>
    <h2>Installation</h2>
    <p>
      To install <a href="https://weasyprint.org/">WeasyPrint</a>, I recommend
      you first
      <a
        href="https://dev.to/bowmanjd/python-tools-for-managing-virtual-environments-3bko"
        >set up a virtual environment with the tool of your choice</a
      >.
    </p>
    <p>
      Then, installation is as simple as performing something like the following
      in an activated virtual environment:
    </p>
    <pre><code class="language-console">pip install weasyprint
</code></pre>
    <p>Alternatives to the above, depending on your tooling:</p>
    <ul>
      <li><code>poetry add weasyprint</code></li>
      <li><code>conda install -c conda-forge weasyprint</code></li>
      <li><code>pipenv install weasyprint</code></li>
    </ul>
    <p>You get the idea.</p>
    <p>
      If you only want the <code>weasyprint</code> command-line tool, you could
      even
      <a
        href="https://dev.to/bowmanjd/how-do-i-install-a-python-command-line-tool-or-script-hint-pipx-3i2"
        >use pipx</a
      >
      and install with <code>pipx install weasyprint</code>. While that would
      not make it very convenient to access as a Python library, if you just
      want to convert web pages to PDFs, that may be all you need.
    </p>
    <h2>A command line tool (Python usage optional)</h2>
    <p>
      Once installed, the <code>weasyprint</code> command line tool is
      available. You can convert an HTML file or a web page to PDF. For
      instance, you could try the following:
    </p>
    <pre><code class="language-console">weasyprint \
&quot;https://en.網(wǎng)址.org/wiki/Python_(programming_language)&quot; \
python.pdf
</code></pre>
    <p>
      The above command will save a file <code>python.pdf</code> in the current
      working directory, converted from the HTML from the
      <a href="https://en.網(wǎng)址.org/wiki/Python_(programming_language)"
        >Python programming language article in English on 網(wǎng)址</a
      >. It ain't perfect, but it gives you an idea, hopefully.
    </p>
    <p>
      You don't have to specify a web address, of course. Local HTML files work
      fine, and they provide necessary control over content and styling.
    </p>
    <pre><code class="language-console">weasyprint sample.html out/sample.pdf
</code></pre>
    <p>
      Feel free to
      <a
        href="https://raw.githubusercontent.com/bowmanjd/pyweasyprintdemo/main/sample.html"
        >download a <code>sample.html</code></a
      >
      and an associated
      <a
        href="https://raw.githubusercontent.com/bowmanjd/pyweasyprintdemo/main/sample.css"
        ><code>sample.css</code> stylesheet</a
      >
      with the contents of this article.
    </p>
    <p>
      See
      <a
        href="https://weasyprint.readthedocs.io/en/latest/tutorial.html#as-a-standalone-program"
        >the WeasyPrint docs</a
      >
      for further examples and instructions regarding the standalone
      <code>weasyprint</code> command line tool.
    </p>
    <h2>Utilizing WeasyPrint as a Python library</h2>
    <p>
      The
      <a href="https://weasyprint.readthedocs.io/">Python API for WeasyPrint</a>
      is quite versatile. It can be used to load HTML when passed appropriate
      file pointers, file names, or the text of the HTML itself.
    </p>
    <p>
      Here is an example of a simple <code>makepdf()</code> function that
      accepts an HTML string, and returns the binary PDF data.
    </p>
    <pre><code class="language-python">from weasyprint import HTML


def makepdf(html):
    &quot;&quot;&quot;Generate a PDF file from a string of HTML.&quot;&quot;&quot;
    htmldoc = HTML(string=html, base_url=&quot;&quot;)
    return htmldoc.write_pdf()
</code></pre>
    <p>
      The main workhorse here is the <code>HTML</code> class. When instantiating
      it, I found I needed to pass a <code>base_url</code> parameter in order
      for it to load images and other assets from relative urls, as in
      <code>&lt;img src=&quot;somefile.png&quot;&gt;</code>.
    </p>
    <p>
      Using <code>HTML</code> and <code>write_pdf()</code>, not only will the
      HTML be parsed, but associated CSS, whether it is embedded in the head of
      the HTML (in a <code>&lt;style&gt;</code> tag), or included in a
      stylesheet (with a
      <code
        >&lt;link href=&quot;sample.css&quot;
        rel=&quot;stylesheet&quot;\&gt;</code
      >
      tag).
    </p>
    <p>
      I should note that <code>HTML</code> can load straight from files, and
      <code>write_pdf()</code> can write to a file, by specifying filenames or
      file pointers. See
      <a href="https://weasyprint.readthedocs.io/">the docs</a> for more detail.
    </p>
    <p>
      Here is a more full-fledged example of the above, with primitive command
      line handling capability added:
    </p>
    <pre><code class="language-python">from pathlib import Path
import sys

from weasyprint import HTML


def makepdf(html):
    &quot;&quot;&quot;Generate a PDF file from a string of HTML.&quot;&quot;&quot;
    htmldoc = HTML(string=html, base_url=&quot;&quot;)
    return htmldoc.write_pdf()


def run():
    &quot;&quot;&quot;Command runner.&quot;&quot;&quot;
    infile = sys.argv[1]
    outfile = sys.argv[2]
    html = Path(infile).read_text()
    pdf = makepdf(html)
    Path(outfile).write_bytes(pdf)


if __name__ == &quot;__main__&quot;:
    run()
</code></pre>
    <p>
      You may
      <a
        href="https://raw.githubusercontent.com/bowmanjd/pyweasyprintdemo/main/weasyprintdemo.py"
        >download the above file</a
      >
      directly, or
      <a href="https://github.com/bowmanjd/pyweasyprintdemo"
        >browse the Github repo</a
      >.
    </p>
    <blockquote>
      <p>
        A note about Python types: the <code>string</code> parameter when
        instantiating <code>HTML</code> is a normal (Unicode) <code>str</code>,
        but <code>makepdf()</code> outputs <code>bytes</code>.
      </p>
    </blockquote>
    <p>
      Assuming the above file is in your working directory as
      <code>weasyprintdemo.py</code> and that a <code>sample.html</code> and an
      <code>out</code> directory are also there, the following should work well:
    </p>
    <pre><code class="language-console">python weasyprintdemo.py sample.html out/sample.pdf
</code></pre>
    <p>
      Try it out, then open <code>out/sample.pdf</code> with your PDF reader.
      Are we close?
    </p>
    <h2>Styling HTML for print</h2>
    <p>
      As is probably apparent, using WeasyPrint is easy. The real work with HTML
      to PDF conversion, however, is in the styling. Thankfully, CSS has pretty
      good support for printing.
    </p>
    <p>Some useful CSS print resources:</p>
    <ul>
      <li>
        <a href="https://css-tricks.com/tag/print-stylesheet/"
          >Various articles on CSS-Tricks</a
        >
      </li>
      <li>
        <a href="https://flaviocopes.com/css-printing/#print-css"
          >A nice summary on flaviocopes</a
        >
      </li>
      <li>
        <a href="https://developer.mozilla.org/en-US/docs/Web/Guide/Printing"
          >The MDN web docs</a
        >
      </li>
    </ul>
    <p>This simple stylesheet demonstrates a few basic tricks:</p>
    <pre><code class="language-css">body {
  font-family: sans-serif;
}
@media print {
  a::after {
    content: &quot; (&quot; attr(href) &quot;) &quot;;
  }
  pre {
    white-space: pre-wrap;
  }
  @page {
    margin: 0.75in;
    size: Letter;
    @top-right {
      content: counter(page);
    }
  }
  @page :first {
    @top-right {
      content: &quot;&quot;;
    }
  }
}
</code></pre>
    <p>
      First, use
      <a
        href="https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries"
        >media queries</a
      >. This allows you to use the same stylesheet for both print and screen,
      using <code>@media print</code> and
      <code>@media screen</code> respectively. In the example stylesheet, I
      assume that the defaults (such as seen in the
      <code>body</code> declaration) apply to all formats, and that
      <code>@media print</code> provides overrides. Alternatively, you could
      include separate stylesheets for print and screen, using the
      <code>media</code> attribute of the <code>&lt;link&gt;</code> tag, as in
      <code
        >&lt;link rel=&quot;stylesheet&quot; src=&quot;print.css&quot;
        media=&quot;print&quot; /&gt;</code
      >.
    </p>
    <p>
      Second,
      <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/@page"
        >use <code>@page</code> CSS rules</a
      >. While
      <a href="https://caniuse.com/mdn-css_at-rules_page_size"
        >browser support</a
      >
      is pretty abysmal in 2020, WeasyPrint does a pretty good job of supporting
      what you need. Note the margin and size adjustments above, and the page
      numbering, in which we first define a counter in the top-right, then
      override with <code>:first</code> to make it blank on the first page only.
      In other words, page numbers only show from page 2 onward.
    </p>
    <p>
      Also note the <code>a::after</code> trick to explicitly display the
      <code>href</code> attribute when printing. This is either clever or
      annoying, depending on your goals.
    </p>
    <p>
      Another hint, not demonstrated above: within the
      <code>@media print</code> block, set <code>display: none</code> on any
      elements that don't need to be printed, and set
      <code>background: none</code> where you don't want backgrounds printed.
    </p>
    <h2>Django and Flask support</h2>
    <p>
      If you write <a href="https://www.djangoproject.com/">Django</a> or
      <a href="https://flask.palletsprojects.com/">Flask</a> apps, you may
      benefit from the convenience of the respective libraries for generating
      PDFs within these frameworks:
    </p>
    <ul>
      <li>
        <a href="https://github.com/fdemmer/django-weasyprint"
          >django-weasyprint</a
        >
        provides a <code>WeasyTemplateView</code> view base class or a
        <code>WeasyTemplateResponseMixin</code> mixin on a TemplateView
      </li>
      <li>
        <a href="https://pythonhosted.org/Flask-WeasyPrint/"
          >Flask-WeasyPrint</a
        >
        provides a special <code>HTML</code> class that works just like
        WeasyPrint's, but respects Flask routes and WSGI. Also provided is a
        <code>render_pdf</code> function that can be called on a template or on
        the <code>url_for()</code> of another view, setting the correct
        mimetype.
      </li>
    </ul>
    <h2>Generate HTML the way you like</h2>
    <p>
      WeasyPrint encourages the developer to make HTML and CSS, and the PDF just
      happens. If that fits your skill set, then you may enjoy experimenting
      with and utilizing this library.
    </p>
    <p><em>How</em> you generate HTML is entirely up to you. You might:</p>
    <ul>
      <li>
        Write HTML from scratch, and use
        <a href="https://jinja.palletsprojects.com/">Jinja templates</a> for
        variables and logic.
      </li>
      <li>
        Write Markdown and convert it to HTML with
        <a href="https://github.com/theacodes/cmarkgfm">cmarkgfm</a> or
        <a
          href="https://dev.to/bowmanjd/processing-markdown-in-python-using-available-commonmark-implementations-cmarkgfm-paka-cmark-and-mistletoe-350a"
          >other Commonmark implementation</a
        >.
      </li>
      <li>
        Generate HTML Pythonically, with
        <a href="https://github.com/Knio/dominate/">Dominate</a> or
        <a href="https://lxml.de/tutorial.html#the-e-factory"
          >lxml's E factory</a
        >
      </li>
      <li>
        Parse, modify, and prettify your HTML (or HTML written by others) with
        <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/"
          >BeautifulSoup</a
        >
      </li>
    </ul>
    <p>Then generate the PDF using WeasyPrint.</p>
    <p>Anything I missed? Feel free to leave comments!</p>
  </body>
</html>

有關(guān)獨立命令行工具的更多示例和說明,請參閱WeasyPrint 文檔。

(https://weasyprint.readthedocs.io/en/latest/tutorial.html#as-a-standalone-program)

weasyprint

使用 WeasyPrint 作為 Python 庫

WeasyPrint 的 Python API非常通用。當(dāng)傳遞適當(dāng)?shù)奈募羔槨⑽募?HTML 本身的文本時,它可用于加載 HTML。

下面是一個簡單makepdf()函數(shù)的示例,它接受 HTML 字符串并返回二進制 PDF 數(shù)據(jù)。

from weasyprint import HTML


def makepdf(html):
    """Generate a PDF file from a string of HTML."""
    htmldoc = HTML(string=html, base_url="")
    return htmldoc.write_pdf()

這里的主要工作是HTML班級。實例化它時,我發(fā)現(xiàn)我需要傳遞一個base_url參數(shù),以便它從相對 URL 加載圖像和其他資源,如<img src="somefile.png">.

使用HTMLand write_pdf(),不僅會解析 HTML,還會解析關(guān)聯(lián)的 CSS,無論它是嵌入 HTML 的頭部(在標(biāo)簽中<style>),還是包含在樣式表中(帶有標(biāo)簽<link href="sample.css" rel="stylesheet"\>)。

我應(yīng)該注意,HTML可以直接從文件加載,并且write_pdf()可以通過指定文件名或文件指針寫入文件。有關(guān)更多詳細(xì)信息,請參閱文檔。

(https://weasyprint.readthedocs.io/)

這是上面的一個更成熟的示例,添加了原始命令行處理功能:

from pathlib import Path
import sys

from weasyprint import HTML


def makepdf(html):
    """Generate a PDF file from a string of HTML."""
    htmldoc = HTML(string=html, base_url="")
    return htmldoc.write_pdf()


def run():
    """Command runner."""
    infile = sys.argv[1]
    outfile = sys.argv[2]
    html = Path(infile).read_text()
    pdf = makepdf(html)
    Path(outfile).write_bytes(pdf)


if __name__ == "__main__":
    run()

您可以直接下載上述文件,或者瀏覽Github repo。(https://github.com/bowmanjd/pyweasyprintdemo)

"""Generate PDF from HTML."""

from pathlib import Path
import sys

from weasyprint import HTML


def makepdf(html):
    """Generate a PDF file from a string of HTML."""
    htmldoc = HTML(string=html, base_url="")
    return htmldoc.write_pdf()


def run():
    """Command runner."""
    infile = sys.argv[1]
    outfile = sys.argv[2]
    html = Path(infile).read_text()
    pdf = makepdf(html)
    Path(outfile).write_bytes(pdf)


if __name__ == "__main__":
    run()


文章來源地址http://www.zghlxwxcb.cn/article/309.html

關(guān)于Python類型的說明:在實例化HTML時,字符串參數(shù)是普通的Unicode str類型,

但是makepdf()方法輸出的是字節(jié)(bytes)類型

假設(shè)上述文件以weasyprintdemo.py的形式存在于您的工作目錄中,并且還有一個sample.html文件和一個名為out的目錄,那么以下內(nèi)容應(yīng)該能夠正常工作:

python weasyprintdemo.py sample.html out/sample.pdf

嘗試一下,然后out/sample.pdf用 PDF 閱讀器打開。我們很親近嗎?

打印 HTML 樣式

顯而易見,使用 WeasyPrint 很容易。然而,HTML 到 PDF 轉(zhuǎn)換的真正工作在于樣式。值得慶幸的是,CSS 對打印有很好的支持。

一些有用的 CSS 打印資源:

  • 有關(guān) CSS 技巧的各種文章 https://css-tricks.com/tag/print-stylesheet/

  • 關(guān)于 flaviocopes 的一個很好的總結(jié) https://flaviocopes.com/css-printing/#print-css

  • MDN 網(wǎng)絡(luò)文檔 https://developer.mozilla.org/en-US/docs/Web/Guide/Printing

這個簡單的樣式表演示了一些基本技巧:

body {
  font-family: sans-serif;
}
@media print {
  a::after {
    content: " (" attr(href) ") ";
  }
  pre {
    white-space: pre-wrap;
  }
  @page {
    margin: 0.75in;
    size: Letter;
    @top-right {
      content: counter(page);
    }
  }
  @page :first {
    @top-right {
      content: "";
    }
  }
}

首先,使用媒體查詢(media queries)。這允許您在打印和屏幕上使用相同的樣式表,分別使用@media print和@media screen。在示例樣式表中,我假設(shè)默認(rèn)值(如body聲明中所見)適用于所有格式,并且@media print提供了覆蓋樣式。或者,您可以使用<link>標(biāo)簽的media屬性,在打印和屏幕上分別包含單獨的樣式表,例如<link rel="stylesheet" src="print.css" media="print" />。

其次,使用@page CSS規(guī)則。雖然2020年瀏覽器支持情況相當(dāng)糟糕,但WeasyPrint在支持所需功能方面做得很好。請注意上述代碼中的邊距和大小調(diào)整以及頁面編號。其中,我們首先在右上角定義一個計數(shù)器,然后使用:first來使其在第一頁上為空白。換句話說,頁碼只會從第二頁開始顯示。

還請注意a::after的技巧,在打印時明確顯示href屬性。這可能要根據(jù)您的目標(biāo)來判斷,有些人可能會認(rèn)為這個技巧很聰明,有些人可能會覺得有些煩人。

另一個提示,上述示例中沒有演示的是:在@media print塊中,將display: none設(shè)置為不需要打印的任何元素,并在不希望背景被打印的地方設(shè)置background: none。

Django 和 Flask 支持

如果您使用Django或Flask應(yīng)用程序,您可能會受益于這些框架中用于生成PDF的方便庫:

  • django-weasyprint提供了一個WeasyTemplateView視圖基類或在TemplateView上提供的WeasyTemplateResponseMixin混合類。

  • Flask-WeasyPrint提供了一個特殊的HTML類,其工作方式與WeasyPrint相同,但同時支持Flask的路由和WSGI。還提供了一個render_pdf函數(shù),可以在模板上調(diào)用該函數(shù),也可以在其他視圖的url_for()上調(diào)用該函數(shù),并設(shè)置正確的MIME類型。

生成HTML的方式完全取決于您。以下是一些可能的方法:

  • 從頭開始編寫HTML,并使用Jinja模板處理變量和邏輯。

  • 使用cmarkgfm或其他Commonmark實現(xiàn)將Markdown轉(zhuǎn)換為HTML。

  • 使用Dominate或lxml的E工廠以Python的方式生成HTML。

  • 使用BeautifulSoup解析、修改和美化您的HTML(或他人編寫的HTML)。

然后使用WeasyPrint生成PDF。

如果我漏掉了什么,請隨時留下評論!


到此這篇關(guān)于使用WeasyPrint將HTML轉(zhuǎn)換為Python PDF生成的文章就介紹到這了,更多相關(guān)內(nèi)容可以在右上角搜索或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

原文地址:http://www.zghlxwxcb.cn/article/309.html

如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請聯(lián)系站長進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 使用Python 實現(xiàn) PDF 到 HTML 的轉(zhuǎn)換

    使用Python 實現(xiàn) PDF 到 HTML 的轉(zhuǎn)換

    PDF 文件是共享和分發(fā)文檔的常用選擇,但提取和再利用 PDF 文件中的內(nèi)容可能會非常麻煩。而利用 Python 將 PDF 文件轉(zhuǎn)換為 HTML?是解決此問題的理想方案之一,這樣做可以增強文檔可訪問性,使文檔可搜索,同時增強文檔在不同場景中的實用性。此外,HTML 格式使得搜索引擎能

    2024年04月08日
    瀏覽(25)
  • java中使用Jsoup和Itext實現(xiàn)將html轉(zhuǎn)換為PDF

    java中使用Jsoup和Itext實現(xiàn)將html轉(zhuǎn)換為PDF

    1.在build.gradle中安裝所需依賴: 2.創(chuàng)建工具類,實現(xiàn)轉(zhuǎn)換方法 3.base64過濾類: 4.字體類代碼,window用戶可在C:windowsfont中尋找自己所需字體即可。我這里用的為黑體: simhei.ttf 效果如下: html頁面預(yù)覽: pdf頁面預(yù)覽: ? ?

    2024年02月14日
    瀏覽(25)
  • 快速轉(zhuǎn)換PDF文件: Python和PyMuPDF教程

    快速轉(zhuǎn)換PDF文件: Python和PyMuPDF教程

    解決問題 有時候?qū)⑽臋n上傳Claude2做分析,有大小限制,所以需要切割pdf文檔為幾個小點的文檔,故才有了本文章。 如何用Python和PyMuPDF制作你想要大小的PDF? PDF是一種廣泛使用的文件格式,可以在任何設(shè)備上查看和打印。但是,有時您可能只需要查看PDF文件中的前幾頁,而

    2024年02月14日
    瀏覽(24)
  • SpringBoot+Thymeleaf 后端轉(zhuǎn)html,pdf HTML生成PDF SpringBoot生成PDF Java PDF生成

    SpringBoot+Thymeleaf 后端轉(zhuǎn)html,pdf HTML生成PDF SpringBoot生成PDF Java PDF生成

    本文詳細(xì)介紹了如何使用SpringBoot和Thymeleaf將后端HTML轉(zhuǎn)換為PDF,包括依賴介紹、模板渲染以及PDF生成等步驟。

    2024年02月09日
    瀏覽(69)
  • 【教程】如何使用Java生成PDF文檔?

    在如今數(shù)字化時代,越來越多的人使用PDF文檔進行信息傳遞和共享。而使用Java生成PDF文檔也成為了一個非常重要的技能,因為Java作為一種通用的編程語言,可以在不同的操作系統(tǒng)和平臺上運行。下面,我們將為您介紹如何使用Java生成PDF文檔。 PDF文檔的生成通常包括兩個步驟

    2024年02月02日
    瀏覽(21)
  • 【PDF】html/dom生成pdf

    【PDF】html/dom生成pdf

    上一篇博客主要講的是pdf文件轉(zhuǎn)換成canvas,然后進行相關(guān)的畫框截圖操作。 【PDF】Canvas繪制PDF及截圖 本篇博客主要講html中dom如何生成pdf文件(前端生成pdf),后端生成pdf當(dāng)然也可以,原理也是將html網(wǎng)頁通過后端服務(wù)導(dǎo)出成pdf,然后css設(shè)置break-after:always;作為分頁邏輯,但是

    2024年02月16日
    瀏覽(25)
  • itextpdf7 使用之 html轉(zhuǎn)pdf,生成目錄可跳轉(zhuǎn)、添加頁眉頁腳

    itextpdf7 使用之 html轉(zhuǎn)pdf,生成目錄可跳轉(zhuǎn)、添加頁眉頁腳

    最近有個需求,生成信用報告。 需求: 1、生成pdf有頁眉頁腳 2、生成目錄 3、目錄加錨點可跳轉(zhuǎn)。 難點: 1、生成的目錄不能實時讀取頁碼 2、目錄是后生成的,屬于兩份pdf拼接的,不能添加錨點跳轉(zhuǎn) 思路: 1、freemaker進行html頁面布局及動態(tài)變量替換 2、生成一份pdf文檔,用

    2024年02月20日
    瀏覽(18)
  • Java使用ftl模板文件生成Word,以及Word轉(zhuǎn)換圖片或Pdf工具類

    Java使用ftl模板文件生成Word,以及Word轉(zhuǎn)換圖片或Pdf工具類

    一、寫在前面 最近在項目中使用打印功能,發(fā)現(xiàn)這個功能我已經(jīng)寫過多次了,下面這個文章的發(fā)步日期在2020年,不得不感慨時間之快啊。 https://blog.csdn.net/weixin_43238452/article/details/109636200?spm=1001.2014.3001.5501 下面介紹一下應(yīng)用場景:這次項目依舊是springboot項目,使用ftl模版生

    2024年02月15日
    瀏覽(38)
  • Vue使用html2canvas將DOM節(jié)點生成對應(yīng)的PDF

    要通過Vue使用html2canvas將DOM節(jié)點生成對應(yīng)的PDF,您需要安裝html2canvas和jspdf這兩個庫。html2canvas用于將DOM節(jié)點轉(zhuǎn)換為Canvas,而jspdf用于將Canvas轉(zhuǎn)換為PDF。以下是一個簡單的示例代碼,展示了如何使用html2canvas和jspdf生成PDF文件: 首先,安裝html2canvas和jspdf依賴: 然后,在Vue組件中

    2024年02月11日
    瀏覽(22)
  • 使用Python將word轉(zhuǎn)換為pdf

    使用Python將word轉(zhuǎn)換為pdf

    使用Python可以將多個word文件一起轉(zhuǎn)為pdf,操作比較便捷,可以實現(xiàn)自動化辦公。 代碼如下: 運行結(jié)果: 歡迎大家查看作者的主頁,主頁中還有關(guān)于編程與算法方面的更多內(nèi)容,歡迎大家相互溝通學(xué)習(xí)。

    2024年02月15日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包