1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
   |  
 
public void Printpdf(Map<String, Object> model,
			Document document, PdfWriter writer, HttpServletRequest request,
			HttpServletResponse response, String link, String filename,  String mnt) throws Exception {
		{
		response.setContentType("application/pdf");
        try 
        {
 
            File fichier =new File(link,filename);
            document = new Document();
            PdfWriter.getInstance(document,  new FileOutputStream(fichier));
 
            Rectangle pagesize = new Rectangle(360f, 720f);
            document.setPageSize(pagesize);
			document.setMargins(10, 10, 10, 10);
 
            document.open();
 
 
            document.setPageSize(pagesize);
            document.setMargins(10, 10, 10, 10);
 
 
                document.newPage();
          	    document.setPageSize(pagesize);
                document.setMargins(10, 10, 10, 10);
                document.newPage();
 
 
                String image = request.getScheme() + "://"  + request.getHeader("Host")  + request.getContextPath()
                      + "/resources/img/logo.png";
 
                Image image1 = Image.getInstance(image);
                PdfPTable tableL = new PdfPTable(2);
                tableL.setWidthPercentage(100);
                tableL.getDefaultCell().setBorder(0);
                tableL.getDefaultCell().setPadding(0);
                tableL.addCell(image);
                tableL.addCell(image1);
                document.add(tableL);
 
 
                Paragraph mode = new Paragraph("Mon projet", FontFactory.getFont(FontFactory.HELVETICA, 14));
 
                PdfPTable table = new PdfPTable(new float[] { 55, 48 });
                table.setWidthPercentage(100);
                table.getDefaultCell().setPadding(0);
                table.getDefaultCell().setBorder(0);
                PdfPCell A = new PdfPCell(new Paragraph(mnt,FontFactory.getFont(FontFactory.HELVETICA, 20)));
                A.setBorder(Rectangle.BOTTOM | Rectangle.TOP);
                A.setHorizontalAlignment(Element.ALIGN_CENTER);
                table1.addCell(A);
                document.add(table);
 
            document.close();
 
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;
            PrintRequestAttributeSet patts = new HashPrintRequestAttributeSet();
		      PrintService[] ps = PrintServiceLookup.lookupPrintServices(flavor, patts);
 
		      if (ps.length == 0) {  throw new IllegalStateException("Aucune imprimante"); }
		      else
		      {
		    	PrintService myService = PrintServiceLookup.lookupDefaultPrintService();
		    	if (myService == null) { throw new IllegalStateException("Aucune imprimante par défaut");  }
		    	else
		    	{
		    		FileInputStream fis = new FileInputStream(fichier);
		    		Doc pdfDoc = new SimpleDoc(fis, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
		    		DocPrintJob printJob = myService.createPrintJob();
		    		printJob.print(pdfDoc, new HashPrintRequestAttributeSet());
      		    fis.close();  
 
      		    fichier.delete();
		    	}
		      }
 
 
        } 
        catch (DocumentException de) {  throw new IOException(de.getMessage());  }
 
		}
 
	} |