Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception thrown when trying out the provided example #10

Closed
pierce-build-user opened this issue May 23, 2017 · 7 comments
Closed

Exception thrown when trying out the provided example #10

pierce-build-user opened this issue May 23, 2017 · 7 comments
Assignees
Labels

Comments

@pierce-build-user
Copy link

Context

Running you example, I would expect it to work, instead I experience the following exception:
Could not find a suitable constructor in com.builtamont.play.pdf.PdfGenerator. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
at com.builtamont.play.pdf.PdfGenerator.class(PdfGenerator.scala:53)
while locating com.builtamont.play.pdf.PdfGenerator

Also, this coed does not compile since your "example" template expect a message, and you call it without.


Definition of Done

Working code, or updated documentation that explain the use of inject into HomeController

@hhandoko
Copy link
Owner

hhandoko commented May 24, 2017

Hi @pierce-build-user,

I've just checked by re-running both Play 2.4 and 2.5 examples and they are running fine. Can you please share how are you running the examples? e.g. steps and commands in SBT.

Regarding passing a message, can you be more specific which part of the code / examples you are referring to? The example.scala.html is a template with no constructor parameter args, e.g. @(). Or perhaps, by message do you mean i18n messages?

@hhandoko hhandoko changed the title Tried to follow t Exception thrown when trying out the provided example May 24, 2017
@hhandoko hhandoko self-assigned this May 24, 2017
@hhandoko hhandoko added the bug label May 24, 2017
@pierce-build-user
Copy link
Author

pierce-build-user commented May 24, 2017

More or less as the example.
I went for import it.innove.play.pdf.PdfGenerator instead.
But to start with I had

resolvers ++= Seq(
  Resolver.sonatypeRepo("snapshots")
)

libraryDependencies ++= Seq(
 "com.builtamont" %% "play2-scala-pdf" % "1.6.1.P25"
)

A view:

@(message: String)

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

And a home controller:

import com.builtamont.play.pdf.PdfGenerator

@Singleton
class HomeController @Inject() (pdfGen: PdfGenerator) extends Controller {    
    def examplePdf = Action { pdfGen.ok(views.html.example(), "http://localhost:9000") }    
}

As the example, which don't compile, since the @message is not used in the call. But even with the message, it complains of having no default constructor

I would guess because the class is defined like this:
class PdfGenerator(env: Environment, val xhtml: Boolean = false)
And an env is required.
But for the play24 version it looked like this:
class PdfGenerator(val xhtml: Boolean = false) {

@hhandoko
Copy link
Owner

hhandoko commented May 24, 2017

I just realised ApplicationModule.scala was not committed as part of the example code, my bad.

The error makes sense now since it's trying to find a resolution for a PdfGenerator instance (but it's missing). It should look something like below:

Play 2.4

package com.builtamont.modules

import com.google.inject.{AbstractModule, Provides}
import net.codingwell.scalaguice.ScalaModule

import com.builtamont.play.pdf.PdfGenerator

class ApplicationModule extends AbstractModule with ScalaModule {

  /** Module configuration + binding */
  def configure(): Unit = {}

  /**
   * Provides PDF generator implementation.
   *
   * @return PDF generator implementation.
   */
  @Provides
  def providePdfGenerator(): PdfGenerator = {
    val pdfGen = new PdfGenerator()
    pdfGen.loadLocalFonts(Seq("fonts/opensans-regular.ttf"))
    pdfGen
  }

}

Play 2.5

package com.builtamont.modules

import com.google.inject.{AbstractModule, Provides}
import net.codingwell.scalaguice.ScalaModule

import play.api.Environment

import com.builtamont.play.pdf.PdfGenerator

class ApplicationModule extends AbstractModule with ScalaModule {

  /** Module configuration + binding */
  def configure(): Unit = {}

  /**
   * Provides PDF generator implementation.
   *
   * @param env The current Play app Environment context.
   * @return PDF generator implementation.
   */
  @Provides
  def providePdfGenerator(env: Environment): PdfGenerator = {
    val pdfGen = new PdfGenerator(env)
    pdfGen.loadLocalFonts(Seq("fonts/opensans-regular.ttf"))
    pdfGen
  }

}

They are both referenced within application.conf:

play {
  # Register the user modules
  modules {
    enabled += "com.builtamont.modules.ApplicationModule"
  }
}

I'll update the repo later today.

@pierce-build-user
Copy link
Author

Ah. Thanks!

@hhandoko
Copy link
Owner

hhandoko commented May 24, 2017

Hi @pierce-build-user ,

I've pushed the repo update as a PR (refer to the above), please have a look. I have to run now but I'll merge the PR to master sometime tonight (if all test passes). Let me know if the examples works OK for you now.

NOTE: Since the package name is updated, you'll need to update your application.conf to reflect it as well.

@hhandoko
Copy link
Owner

Merged in master.

@hhandoko
Copy link
Owner

Hi @pierce-build-user , let me know if you still have any problem... Otherwise I'll automatically close this in 48 hours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants