Crea un PDF a través de Python

Creación de archivos PDF nativos y de alto rendimiento sin la instalación de Adobe Acrobat mediante Python

Cómo generar un archivo PDF a través de Python

Para crear un PDF, una API potente y fácil de usar, use Aspose.PDF for Python vía .NET. Abra PyPI, instálelo y busque aspose-pdf. También puede ejecutar el comando:

Console

pip install aspose-pdf

Cómo crear un PDF a través de Python


Es fácil para los desarrolladores crear, cargar, modificar y convertir archivos PDF directamente desde la aplicación Python for .NET en solo unas pocas líneas de código.

  1. Incluir el espacio de nombres en el archivo de clase
  2. Inicialice el objeto de la clase Document.
  3. Agregue una página usando el método Pages.Add ().
  4. Crea un nuevo objeto TextFragment y establece su texto.
  5. Agregue TextFragment a la colección Paragraphs de la página.
  6. Guarde el PDF utilizando el método Save (String).

El siguiente código fuente muestra cómo crear un archivo PDF con Python

Este código de ejemplo muestra cómo crear un PDF con Python

import aspose.pdf as apdf

from os import path
path_outfile = path.join(self.data_dir, outfile)

# Initialize document object
document = apdf.Document()
# Add page
page = document.pages.add()
# Add text to new page
textFragment = apdf.text.TextFragment("Hello, world!")
textFragment.position = apdf.text.Position(100, 600)

textFragment.text_state.font_size = 12
textFragment.text_state.font = apdf.text.FontRepository.find_font(
    "TimesNewRoman"
)
textFragment.text_state.background_color = apdf.Color.blue
textFragment.text_state.foreground_color = apdf.Color.yellow

# Create TextBuilder object
textBuilder = apdf.text.TextBuilder(page)

# Append the text fragment to the PDF page
textBuilder.append_text(textFragment)

document.save(path_outfile)