// --- GENERATE PDF DOCUMENT BYTES -------------------------------------------------------------
  public ByteArrayOutputStream generatePDFDocumentBytes(
      final HttpServletRequest req, StringBuffer patientids, String printlanguage)
      throws Exception {
    sPrintLanguage = printlanguage;
    ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
    docWriter = PdfWriter.getInstance(doc, baosPDF);
    docWriter.setPageEvent(new AMCEndPage());
    this.req = req;

    String sURL = req.getRequestURL().toString();
    if (sURL.indexOf("openclinic/") > 0) {
      sURL = sURL.substring(0, sURL.indexOf("openclinic/"));
    }

    String sContextPath = req.getContextPath() + "/";
    HttpSession session = req.getSession();
    String sProjectDir = (String) session.getAttribute("activeProjectDir");

    this.url = sURL;
    this.contextPath = sContextPath;
    this.projectDir = sProjectDir;

    try {
      doc.addProducer();
      doc.addAuthor(user.person.firstname + " " + user.person.lastname);
      doc.addCreationDate();
      doc.addCreator("OpenClinic Software");
      Rectangle rectangle =
          new Rectangle(
              0,
              0,
              new Float(MedwanQuery.getInstance().getConfigInt("patientCardWidth", 540) * 72 / 254)
                  .floatValue(),
              new Float(MedwanQuery.getInstance().getConfigInt("patientCardHeight", 860) * 72 / 254)
                  .floatValue());
      doc.setPageSize(rectangle.rotate());
      doc.setMargins(10, 10, 10, 10);
      doc.setJavaScript_onLoad("print();\r");
      doc.open();

      // add content to document
      String[] persons = patientids.toString().split(";");
      for (int n = 0; n < persons.length; n++) {
        AdminPerson person = DatacenterHelper.getPatientRecord(persons[n]);
        if (person != null) {
          printPatientCard(person);
          doc.newPage();
        }
      }
    } catch (Exception e) {
      baosPDF.reset();
      e.printStackTrace();
    } finally {
      if (doc != null) doc.close();
      if (docWriter != null) docWriter.close();
    }

    if (baosPDF.size() < 1) {
      throw new DocumentException("document has no bytes");
    }

    return baosPDF;
  }
  protected void printPatientCard(AdminPerson person) {
    try {
      table = new PdfPTable(1000);
      table.setWidthPercentage(100);
      PdfPTable table2 = new PdfPTable(1000);
      table.setWidthPercentage(100);

      // Professional card
      cell = createLabel(" ", 32, 1, Font.BOLD);
      cell.setColspan(1000);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
      cell.setPadding(0);
      table2.addCell(cell);

      cell = new PdfPCell(table2);
      cell.setColspan(1000);
      cell.setBorder(PdfPCell.NO_BORDER);
      table.addCell(cell);
      cell = createValueCell(" ");
      cell.setColspan(1000);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPadding(0);
      table.addCell(cell);

      table2 = new PdfPTable(1000);
      // Name
      cell =
          createLabel(
              ScreenHelper.getTranNoLink("web", "lastname", sPrintLanguage) + ":",
              6,
              1,
              Font.ITALIC);
      cell.setColspan(300);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPadding(1);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      table2.addCell(cell);
      cell = createLabel(person.lastname, 8, 1, Font.BOLD);
      cell.setColspan(700);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPadding(1);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      table2.addCell(cell);

      // FirstName
      cell =
          createLabel(
              ScreenHelper.getTranNoLink("web", "firstname", sPrintLanguage) + ":",
              6,
              1,
              Font.ITALIC);
      cell.setColspan(300);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPadding(1);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      table2.addCell(cell);
      cell = createLabel(person.firstname, 8, 1, Font.BOLD);
      cell.setColspan(700);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPadding(1);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      table2.addCell(cell);

      // Date of birth
      cell =
          createLabel(
              ScreenHelper.getTranNoLink("web", "dateofbirth", sPrintLanguage) + ":",
              6,
              1,
              Font.ITALIC);
      cell.setColspan(300);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPadding(1);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      table2.addCell(cell);
      cell = createLabel(person.dateOfBirth, 8, 1, Font.BOLD);
      cell.setColspan(700);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPadding(1);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      table2.addCell(cell);

      // Registration number
      cell =
          createLabel(
              ScreenHelper.getTranNoLink("web", "idcode", sPrintLanguage) + ":", 6, 1, Font.ITALIC);
      cell.setColspan(300);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      cell.setPadding(1);
      table2.addCell(cell);
      cell = createLabel(person.comment, 8, 1, Font.BOLD);
      cell.setColspan(700);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPadding(1);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      table2.addCell(cell);

      // Barcode
      PdfContentByte cb = docWriter.getDirectContent();
      Barcode39 barcode39 = new Barcode39();
      barcode39.setCode("A" + person.comment);
      barcode39.setAltText("");
      barcode39.setSize(1);
      barcode39.setBaseline(0);
      barcode39.setBarHeight(20);
      Image image = barcode39.createImageWithBarcode(cb, null, null);
      cell = new PdfPCell(image);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setVerticalAlignment(PdfPCell.ALIGN_TOP);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      cell.setColspan(1000);
      cell.setPadding(0);
      table2.addCell(cell);

      cell = new PdfPCell(table2);
      cell.setColspan(700);
      cell.setBorder(PdfPCell.NO_BORDER);
      table.addCell(cell);

      // Photo
      image = null;
      try {
        image = Image.getInstance(DatacenterHelper.getPatientPicture(person.comment));
      } catch (Exception e1) {
        e1.printStackTrace();
      }
      if (image != null) {
        image.scaleToFit(130, 72);
        cell = new PdfPCell(image);
      } else {
        cell = new PdfPCell();
      }
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
      cell.setColspan(300);
      cell.setPadding(0);
      table.addCell(cell);

      // Horizontal line
      cell = new PdfPCell();
      cell.setBorder(PdfPCell.BOTTOM);
      cell.setColspan(1000);
      table.addCell(cell);

      table2 = new PdfPTable(2000);
      cell =
          createLabel(
              ScreenHelper.getTranNoLink("web", "deliverydate", sPrintLanguage), 6, 1, Font.ITALIC);
      cell.setColspan(550);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPaddingRight(5);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
      table2.addCell(cell);
      cell = createLabel(ScreenHelper.stdDateFormat.format(new java.util.Date()), 6, 1, Font.BOLD);
      cell.setColspan(450);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      cell.setPadding(0);
      table2.addCell(cell);

      // Expiry data
      cell =
          createLabel(
              ScreenHelper.getTranNoLink("web", "expirydate", sPrintLanguage), 6, 1, Font.ITALIC);
      cell.setColspan(550);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPaddingRight(5);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
      table2.addCell(cell);
      long day = 24 * 3600 * 1000;
      long year = 365 * day;
      long period = MedwanQuery.getInstance().getConfigInt("cardvalidityperiod", 5) * year - day;
      cell =
          createLabel(
              ScreenHelper.stdDateFormat.format(
                  new java.util.Date(new java.util.Date().getTime() + period)),
              6,
              1,
              Font.BOLD);
      cell.setColspan(450);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      cell.setPadding(0);
      table2.addCell(cell);

      cell = new PdfPCell(table2);
      cell.setColspan(1000);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
      cell.setPadding(1);
      table.addCell(cell);

      cell =
          createLabel(
              ScreenHelper.getTranNoLink("web", "cardfooter2", sPrintLanguage), 6, 1, Font.ITALIC);
      cell.setColspan(1000);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
      cell.setPadding(0);
      table.addCell(cell);

      doc.add(table);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }