rjcossa
rjcossa

Reputation: 117

PDFBox creates PDF that is displayed differently in each browser

I am using PDFBox to create PDFs and i insert a "background" image on the PDF which is basically a curved border and some social network information.

Please find below the code i use to generate the PDF:

 PDDocument document = new PDDocument();
    PDPage page = new PDPage();

    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);

    PDImageXObject pdImage = PDImageXObject.createFromFile(basePath+"images/"+ticket.getId()+".png", document);
    PDImageXObject backgroundImage = PDImageXObject.createFromFile(basePath+"images/ticket_template.jpg",document);

    backgroundImage.setWidth(595);
    backgroundImage.setHeight(800);

    contentStream.drawImage(backgroundImage,10,0);
    contentStream.drawImage(pdImage,(page.getMediaBox().getWidth() - pdImage.getWidth()) / 2,page.getMediaBox().getHeight()-400);

    float margin = 50;
    // starting y position is whole page height subtracted by top and bottom margin
    float yStartNewPage = page.getMediaBox().getHeight() - (2 * margin);
    // we want table across whole page width (subtracted by left and right margin ofcourse)
    float tableWidth = page.getMediaBox().getWidth() - (2 * margin);

    boolean drawContent = true;
    float yStart = yStartNewPage;
    float bottomMargin = 70;
    // y position is your coordinate of top left corner of the table
    float yPosition = 550;
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");

    BaseTable table = new BaseTable(400, yStartNewPage, bottomMargin, tableWidth, margin, document, page, true, drawContent);
    generateTable(table,"Event",event.getName());
    generateTable(table,"Código",ticket.getId());
    generateTable(table,"Data Ínicio",dateFormat.format(event.getStartDate()));
    generateTable(table,"Data Fim",dateFormat.format(event.getEndDate()));
    generateTable(table,"Mesa",eventTable.getName());
    generateTable(table,"Número de Convidados",ticket.getNumberOfPeople()+"");

    table.draw();

    contentStream.close();

    document.save(new File(basePath+"docs/"+ticket.getId()+".pdf"));

    document.close();

When the image is opened it chrome it seems to be misplaced:

Image on chrome

The second image is how it appears in google chrome

I also seem to have issues inserting images with colors to the PDF and they also appear weird when converting the PDF to an image

Upvotes: 0

Views: 487

Answers (1)

minus
minus

Reputation: 2786

Browser approximate PDF rendering up to a point, this means the almost no browser implements exhaustively PDF specs.

This applies to almost every PDF rendering engine, the most truthful is naturally Adobe PDF reader, that is practically the reference implementation.

In the past browser used 3rd party plugins to embed PDF, but that brought security issues, to the point that now they do it themselves.

So you cannot completely rely on a single browser to be sure that your PDF will show correctly every time, it's a trial and error process ( that is also true for other software implementing PDF rendering, like PDF capable printers ).

The only sensible solution is to:

  1. Try it first on Acrobat (it's at least the reference)
  2. Check it with browsers and if they don't comply try to achieve the same result with different techniques.

PDF it's a very complex spec and there are a lot of different ways to get some result, try the simplest.

Upvotes: 1

Related Questions