Skip to content

Instantly share code, notes, and snippets.

@asazorojas
Last active April 11, 2017 13:55
Show Gist options
  • Save asazorojas/b50c3550bd2cc296002b32be248acbea to your computer and use it in GitHub Desktop.
Save asazorojas/b50c3550bd2cc296002b32be248acbea to your computer and use it in GitHub Desktop.
buildscript {
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.14.1"
classpath "org.grails.plugins:hibernate5:${gormVersion-".RELEASE"}"
}
}
version "0.1"
group "worddocument"
apply plugin:"eclipse"
apply plugin:"idea"
apply plugin:"war"
apply plugin:"org.grails.grails-web"
apply plugin:"org.grails.grails-gsp"
apply plugin:"asset-pipeline"
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencies {
compile "org.docx4j:docx4j-ImportXHTML:3.2.1"
compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.grails:grails-core"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails:grails-dependencies"
compile "org.grails:grails-web-boot"
compile "org.grails.plugins:cache"
compile "org.grails.plugins:scaffolding"
compile "org.grails.plugins:hibernate5"
compile "org.hibernate:hibernate-core:5.1.3.Final"
compile "org.hibernate:hibernate-ehcache:5.1.3.Final"
console "org.grails:grails-console"
profile "org.grails.profiles:web"
runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.14.1"
runtime "com.h2database:h2"
testCompile "org.grails:grails-plugin-testing"
testCompile "org.grails.plugins:geb"
testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
}
bootRun {
jvmArgs('-Dspring.output.ansi.enabled=always')
addResources = true
}
assets {
minifyJs = true
minifyCss = true
}
package worddocument
import org.docx4j.convert.in.xhtml.XHTMLImporterImpl
import org.docx4j.openpackaging.packages.WordprocessingMLPackage
import org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart
import javax.servlet.ServletOutputStream
class FirstController {
def index() {
generatePDF(System.getProperty("user.dir") + "/sample.html", System.getProperty("user.dir") + "/html_output.doc")
}
private void generatePDF(String inputFilePath, String outputFilePath) {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
ndp.unmarshalDefaultNumbering();
XHTMLImporterImpl xHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
xHTMLImporter.setHyperlinkStyle("Hyperlink");
if (inputFilePath.endsWith("html")) {
wordMLPackage.getMainDocumentPart().getContent().addAll(
xHTMLImporter.convert(new File(inputFilePath), null) );
}
response.setContentType("application/msword")
response.setHeader('Content-Disposition', 'inline; filename="WordDocument.doc"')
ServletOutputStream outputStream = response.getOutputStream()
wordMLPackage.save(outputStream)
outputStream.flush()
outputStream.close()
// wordMLPackage.save(new File(System.getProperty("user.dir") + "/html_output.docx") );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment