Rendering-Optionen – HTML-Rendering anpassen

Die Rendering-Optionen bieten eine detaillierte Kontrolle darüber, wie das Rendering-Gerät Inhalte verarbeitet. Jedes Gerät – PdfDevice, XpsDevice, DocDevice und ImageDevice – hat seine eigene Klasse von Optionen: PdfRenderingOptions, XpsRenderingOptions, DocRenderingOptions, und ImageRenderingOptions. Mit diesen Optionen können Sie Seitengröße, Ränder und Farben anpassen sowie gerätespezifische Einstellungen vornehmen, z. B. das Hinzufügen eines Sicherheitskennworts zu PDF-Dateien. Die Anpassung ist entscheidend, um die gewünschten Ergebnisse zu erzielen, sei es die Reduzierung der Dateigröße durch Anpassung der Bildqualität und -auflösung oder die Verbesserung der Lesbarkeit durch optimiertes Seitenlayout und Textformatierung.

In diesem Artikel werden die Rendering-Optionen in Aspose.HTML for Python via .NET untersucht. Es wird gezeigt, wie die Konvertierung von HTML-Inhalten in Ausgabeformate wie PDF, XPS, DOCX und Bilder angepasst werden kann. Diese Optionen umfassen allgemeine Einstellungen wie Seitengröße, Hintergrundfarbe und Auflösung sowie gerätespezifische Einstellungen wie Bildkomprimierung, PDF-Sicherheit und Schrifteinbettung, die eine vollständige Kontrolle über die endgültige Ausgabe ermöglichen.

Hier finden Sie Python-Code-Beispiele, die die Verwendung von gerätspezifischen Parametern demonstrieren. Die Verwendung allgemeiner Optionen wird im Kapitel “How-to-Artikel” (in Vorbereitung) behandelt.

Um mehr über den Rendering-Prozess zu erfahren, lesen Sie bitte den Artikel Rendering Device.

PdfRenderingOptions

Die Klasse PdfRenderingOptions unterstützt neben allgemeinen Optionen mehrere spezifische Parameter, wie jpeg_quality, document_info, encryption, form_field_behaviour und is_tagged_pdf.

PropertyDescription
jpeg_qualitySpecifies the quality of JPEG compression for images. The default value is 95.
document_infoThis property contains information about the output PDF document.
encryptionThis property gets or sets encryption details. If it is not set, then no encryption will be performed.
form_field_behaviourThis property specifies the behavior of form fields in the output PDF document.
is_tagged_pdfIf is_tagged_pdf = true, a tag structure will be added to the PDF document during rendering.

Die Eigenschaft form_field_behaviour wird verwendet, um das Verhalten von Formularfeldern in einem PDF-Dokument festzulegen. Was es bedeutet, eine PDF-Datei zu reduzieren, und wie man dies mit der Aspose.HTML for Python via .NET-Bibliothek macht, erfahren Sie im Abschnitt HTML-zu-PDF-Konvertierung und PDF-Flattening.

Der folgende Python-Code zeigt, wie eine PDF-Ausgabedatei mit Hilfe der Klasse PdfRenderingOptions verschlüsselt werden kann:

 1# Render HTML to password-protected PDF in Python
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.rendering as rn
 6import aspose.html.rendering.pdf as rp
 7import aspose.html.rendering.pdf.encryption as rpe
 8
 9# Prepare input and output paths
10data_dir = "data"
11output_dir = "output"
12os.makedirs(output_dir, exist_ok=True)
13
14document_path = os.path.join(data_dir, "document.html")
15save_path = os.path.join(output_dir, "convert-html-to-pdf-with-encryption.pdf")
16
17# Load the HTML document
18with ah.HTMLDocument(document_path) as document:
19    with rn.HtmlRenderer() as renderer:
20        # Configure password-protected PDF output
21        options = rp.PdfRenderingOptions()
22        options.encryption = rpe.PdfEncryptionInfo(
23            user_password="user_pwd",
24            owner_password="owner_pwd",
25            permissions=rpe.PdfPermissions.PRINT_DOCUMENT,
26            encryption_algorithm=rpe.PdfEncryptionAlgorithm.RC4_128
27        )
28
29        # Render HTML to PDF
30        with rp.PdfDevice(options, save_path) as device:
31            renderer.render(device, document)

Das obige Beispiel zeigt, wie Sie eine neue PdfRenderingOptions-Instanz erstellen und Verschlüsselungsoptionen für eine PDF-Ausgabedatei festlegen. Zu diesem Zweck müssen Sie ein PdfEncryptionInfo-Objekt erstellen, das die Verschlüsselungseinstellungen für die PDF-Datei definiert. Der Konstruktor benötigt vier Parameter:

ImageRenderingOptions

Die Klasse ImageRenderingOptions unterstützt alle allgemeinen Optionen und ermöglicht die Konfiguration spezifischer Optionen wie Anti-Aliasing, Text-Rendering-Konfiguration, Bildformatauswahl und Bildkomprimierung.

PropertyDescription
compressionSets Tagged Image File Format (TIFF) compression. By default, this property is LZW.
formatSets the ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default, this property is PNG.
use_antialiasingThis property defines the rendering quality of the image by controlling the use of antialiasing. Antialiasing is enabled by default.
textGets a TextOptions object which is used for configuration of text rendering.

Betrachten wir, wie man ein spezielles ImageRenderingOptions-Objekt verwendet, um das TIFF-Format und die Komprimierung für das Bild festzulegen:

 1# Render HTML to TIFF in Python with custom settings
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.rendering.image as ri
 6
 7# Prepare input and output paths
 8data_dir = "data"
 9output_dir = "output"
10os.makedirs(output_dir, exist_ok=True)
11
12document_path = os.path.join(data_dir, "html_file.html")
13save_path = os.path.join(output_dir, "document.tiff")
14
15# Load the HTML document
16with ah.HTMLDocument(document_path) as document:
17    # Configure TIFF output
18    options = ri.ImageRenderingOptions(ri.ImageFormat.TIFF)
19    options.compression = ri.Compression.NONE
20
21    # Render HTML to TIFF
22    with ri.ImageDevice(options, save_path) as device:
23        document.render_to(device)

XPS-Rendering-Optionen

XpsRenderingOptions erben alle Einstellungen von der Basisklasse RenderingOptions. Sie können Seitengröße, Ränder, Hintergrundfarbe und andere gängige Optionen für die Erzeugung von XPS-Dateien konfigurieren.

 1# Render HTML to XPS in Python with custom page settings
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.rendering.xps as rx
 6import aspose.html.drawing as dr
 7
 8# Prepare input and output paths
 9data_dir = "data"
10output_dir = "output"
11os.makedirs(output_dir, exist_ok=True)
12
13document_path = os.path.join(data_dir, "document.html")
14save_path = os.path.join(output_dir, "document-options.xps")
15
16# Load the HTML document
17with ah.HTMLDocument(document_path) as document:
18    # Configure XPS output
19    options = rx.XpsRenderingOptions()
20    options.page_setup.any_page = dr.Page(
21        dr.Size(dr.Length.from_inches(5.0), dr.Length.from_inches(2.0))
22    )
23
24    # Render HTML to XPS
25    with rx.XpsDevice(options, save_path) as device:
26        document.render_to(device)

DOC-Rendering-Optionen

Die Klasse DocRenderingOptions unterstützt alle allgemeinen Optionen und ermöglicht es Ihnen, die Eigenschaften font_embedding_rule und document_format für die Ausgabedatei anzupassen.

PropertyDescription
font_embedding_ruleThis property gets or sets the font embedding rule. Available values are Full and None. The default value is None.
document_formatThis property gets or sets the file format of the output document. The default value is DOCX.

Das folgende Beispiel zeigt, wie Sie die Rendering-Optionen für ein DOCX-Ausgabedokument anpassen können, indem Sie die Regel für die Seitengröße und die Schrifteinbettung festlegen:

 1# Render HTML to DOCX in Python with custom settings
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.rendering.doc as rd
 6import aspose.html.drawing as dr
 7
 8# Prepare input and output paths
 9data_dir = "data"
10output_dir = "output"
11os.makedirs(output_dir, exist_ok=True)
12
13document_path = os.path.join(data_dir, "nature.html")
14save_path = os.path.join(output_dir, "nature-options.docx")
15
16# Load the HTML document
17with ah.HTMLDocument(document_path) as document:
18    # Configure DOCX output
19    options = rd.DocRenderingOptions()
20    options.page_setup.any_page = dr.Page(
21        dr.Size(dr.Length.from_inches(8.0), dr.Length.from_inches(10.0))
22    )
23    options.font_embedding_rule = rd.FontEmbeddingRule.FULL
24
25    # Render HTML to DOCX
26    with rd.DocDevice(options, save_path) as device:
27        document.render_to(device)

Aspose.HTML bietet kostenlose Online- Konverter, die HTML-, XHTML-, MHTML-, EPUB-, XML- und Markdown-Dateien in eine Reihe von gängigen Formaten konvertieren können. Sie können Ihre HTML-basierten Dokumente ganz einfach in PDF, XPS, DOCX, JPG, PNG, GIF, TIFF und andere konvertieren. Wählen Sie einfach eine Datei und das zu konvertierende Format aus, und schon sind Sie fertig.

Text “HTML-Webanwendungen”