Stampa di PDF tramite Python

Come stampare PDF usando la libreria Python

Come stampare PDF usando la libreria Python

Per stampare file PDF, utilizzeremo l’API Aspose.PDF for .NET, un’API di manipolazione dei documenti ricca di funzionalità, potente e facile da usare per la piattaforma python-net. Apri il gestore di pacchetti NuGet, cerca Aspose.pdf e installa. È inoltre possibile utilizzare il seguente comando dalla console di Package Manager.

Python Package Manager Console

pip install aspose-pdf

Stampa di documenti PDF tramite Python


È necessario Aspose.PDF for .NET per provare il codice nel proprio ambiente.

  1. Carica il PDF con un’istanza di Document.
  2. Ottenere DocumentInfo utilizzando la proprietà Document.
  3. Accedere e visualizzare diverse proprietà Document.Info.

Stampa PDF - Python

<% print.code-block.subtitle %>

    import aspose.pdf as ap
    import aspose.pydrawing as drawing

    # Create PdfViewer object
    viewer = ap.facades.PdfViewer()

    # Open input PDF file
    viewer.bind_pdf("input.pdf")

    # Set attributes for printing
    # Print the file with adjusted size
    viewer.auto_resize = True
    # Print the file with adjusted rotation
    viewer.auto_rotate = True
    # Do not produce the page number dialog when printing
    viewer.print_page_dialog = False

    # Create objects for printer and page settings
    ps = drawing.printing.PrinterSettings()
    pgs = drawing.printing.PageSettings()

    # Set XPS/PDF printer name
    ps.printer_name = "Microsoft XPS Document Writer"
    # Or set the PDF printer
    # ps.printer_name = "Adobe PDF"

    # Set PageSize(if required)
    pgs.paper_size = drawing.printing.PaperSize("A4", 827, 1169)

    # Set PageMargins(if required)
    pgs.margins = drawing.printing.Margins(0, 0, 0, 0)

    # Print document using printer and page settings
    viewer.print_document_with_settings(pgs, ps)

    # Close the PDF file after printing
    viewer.close()