值toBytes不是对象的成员it.innove.play.pdf.PdfGenerator play framework 2.4

时间:2016-07-11 14:00:08

标签: scala pdf playframework

我正在尝试实现这个示例:https://knoldus.wordpress.com/2016/05/20/play2-pdf-plugin-js-enabled-and-disabled-browser/?iframe=true&theme_preview=true在我的应用程序中生成pdf所以这是我的控制器scala:

class GOL {
  int w = 8;
  int columns, rows;
  int[][] board;

  GOL() {
    // Initialize rows, columns and set-up arrays
    columns = width / w;
    rows = height / w;
    board = new int[columns][rows];
    //next = new int[columns][rows];
    // Call function to fill array with random values 0 or 1
    init();
  }

  void init() {
    for (int i = 1; i < columns - 1; i++) {
      for (int j = 1; j < rows - 1; j++) {
        board[i][j] = (int) random(2);
      }
    }
  }

  // The process of creating the new generation
  void generate() {

    int[][] next = new int[columns][rows];

    // Loop through every spot in our 2D array and check spots neighbors
    for (int x = 1; x < columns - 1; x++) {
      for (int y = 1; y < rows - 1; y++) {

        // Add up all the states in a 3x3 surrounding grid
        int neighbors = 0;
        for (int i = -1; i <= 1; i++) {
          for (int j = -1; j <= 1; j++) {
            neighbors += board[x + i][y + j];
          }
        }

        // A little trick to subtract the current cell's state since
        // we added it in the above loop
        neighbors -= board[x][y];

        // Rules of Life
        if ((board[x][y] == 1) && (neighbors < 2)) next[x][y] = 0;
        else if ((board[x][y] == 1) && (neighbors > 3)) next[x][y] = 0;
        else if ((board[x][y] == 0) && (neighbors == 3)) next[x][y] = 1;
        else next[x][y] = board[x][y];
      }
    }

    // Next is now our board
    board = next;
  }

  // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0'
  void display() {
    for (int i = 0; i < columns; i++) {
      for (int j = 0; j < rows; j++) {
        if ((board[i][j] == 1)) fill(0);
        else fill(255);
        stroke(0);
        rect(i * w, j * w, w, w);
      }
    }
  }
}

我的索引:

package controllers

import it.innove.play.pdf.PdfGenerator

import javax.inject.{ Inject, Named }
import com.mohiva.play.silhouette.api.{ Environment, LogoutEvent, Silhouette }
import com.mohiva.play.silhouette.impl.authenticators.CookieAuthenticator
import play.api.i18n.{ I18nSupport, MessagesApi, Messages }
import play.api._
import play.api.mvc._
import scala.concurrent.Future
import play.api.libs.json._
import play.api.Play.current
import models.silhouette.User
import scala.util.{ Try, Success, Failure }
import forms._
import utils._

import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.Configuration

class Application @Inject() (val messagesApi: MessagesApi,
  val env: Environment[User, CookieAuthenticator],
  configuration: Configuration) extends Silhouette[User, CookieAuthenticator] with I18nSupport {

  def homepage: Action[AnyContent] = Action { implicit request =>
    Ok(PdfGenerator.toBytes(views.html.index("Your PDF is generated"), "http://localhost:9000")).as("application/pdf")
  }
}

printPdf:

@(message: String)
@printPdf("Welcome") {
    Image: <img src="/public/images/favicon.png"/><br/>
    Hello world! <br/>
    @message
}

我的依赖项:

@(title: String)(content: Html)
<html lang="en">
    <head>
        <title>@title</title>
        <link rel="stylesheet" media="print"
            href="@routes.Assets.versioned("stylesheets/main.css")">
        </head>
        <body>
            <div>

            </div>
            <div class="content-text" >
                @content
            </div>
            <div>

            </div>
        </body>
    </html>

1 个答案:

答案 0 :(得分:0)

您尝试静态访问非静态方法。 PdfGenerator.toBytes(???)不是静态方法。将其更改为new PdfGenerator().toBytes(???)可以解决您的编译问题。在你的情况下:

def homepage: Action[AnyContent] = Action { implicit request =>
  Ok(new PdfGenerator().toBytes(views.html.index("Your PDF is generated"), "http://localhost:9000")).as("application/pdf")
}