Impression d’un PDF via Python

Comment imprimer un PDF à l’aide de la bibliothèque Python

Comment imprimer un PDF à l'aide de la bibliothèque Python

Pour imprimer un fichier PDF, nous utiliserons l’API Aspose.PDF pour .NET qui est une API de manipulation de documents riche en fonctionnalités, puissante et facile à utiliser pour la plateforme python-net. Ouvrez le gestionnaire de packages NuGet, recherchez ASPOSE.pdf et installez-le. Vous pouvez également utiliser la commande suivante depuis la console du gestionnaire de packages.

Python Package Manager Console

pip install aspose-pdf

Impression d’un document PDF via Python


Vous devez Aspose.PDF for .NET pour essayer le code dans votre environnement.

  1. Chargez le PDF avec une instance de Document.
  2. Obtenez DocumentInfo à l’aide de la propriété Document.Info.
  3. Accédez et affichez différentes propriétés de Document.Info.

Imprimer le 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()