{"id":247,"date":"2024-04-11T08:26:46","date_gmt":"2024-04-11T08:26:46","guid":{"rendered":"https:\/\/vkrastev1blog.com\/?p=247"},"modified":"2024-04-11T08:26:46","modified_gmt":"2024-04-11T08:26:46","slug":"how-to-create-pdf-from-html-or-image-using-java-pdfbox-framework","status":"publish","type":"post","link":"https:\/\/vkrastev1blog.com\/2024\/04\/11\/how-to-create-pdf-from-html-or-image-using-java-pdfbox-framework\/","title":{"rendered":"Kako ustvariti PDF iz HTML ali slike z uporabo java pdfbox frameworka"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Tukaj je naslednji primer kode:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>In pom.xml insert or get the jar files and add them to the java project if you don't use maven \n&lt;repositories&gt;\n  &lt;repository&gt;\n    &lt;id&gt;jitpack.io&lt;\/id&gt;\n    &lt;url&gt;https:\/\/jitpack.io&lt;\/url&gt;\n  &lt;\/repository&gt;\n&lt;\/repositories&gt;\n&lt;!-- https:\/\/mvnrepository.com\/artifact\/org.apache.pdfbox\/pdfbox --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.apache.pdfbox&lt;\/groupId&gt;\n    &lt;artifactId&gt;pdfbox&lt;\/artifactId&gt;\n    &lt;version&gt;3.0.2&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;!-- https:\/\/mvnrepository.com\/artifact\/com.github.hkirk\/java-html2image --&gt;\n&lt;dependency&gt;\n   &lt;groupId&gt;com.github.hkirk&lt;\/groupId&gt;\n   &lt;artifactId&gt;java-html2image&lt;\/artifactId&gt;\n   &lt;version&gt;0.9&lt;\/version&gt;\n&lt;\/dependency&gt;\n\nand Java code here \npublic static void main(String&#091;] args) {\n            String html = \"&lt;html lang=\\\"en\\\" xmlns=\\http:\/\/www.w3.org\/1999\/xhtml\\ style=\\\"cursor: default;\\\"&gt;\"\n                        + \"&lt;body id=\\\"body\\\" class=\\\"modal-open\\\" style=\\\"cursor: default; padding-right: 15px;\\\"&gt;\\r\\n\"\n                        + \"&lt;p&gt;\" + \"Hello World&lt;\/p&gt;\";\n                        + \"&lt;\/body&gt;\\r\\n\" + \"&lt;\/html&gt;\\r\\n\" + \"\\r\\n\" + \"\";\n            HtmlImageGenerator imageGenerator = new HtmlImageGenerator();\n            imageGenerator.loadHtml(html);\n            BufferedImage bimg = imageGenerator.getBufferedImage();\n            float width = bimg.getWidth();\n            float height = bimg.getHeight();\n            PDPage page = new PDPage(new PDRectangle(width, height));\n            PDDocument document = new PDDocument();\n            document.addPage(page);\n            try {\n                  PDImageXObject pdImage = LosslessFactory.createFromImage(document, bimg);\n\n                  try (PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.OVERWRITE, true,\n                              true)) {\n                        contentStream.drawImage(pdImage, 0, 0, width, height);\n                  }\n                  document.save(\"xx.pdf\");\n                  document.close();\n            } catch (IOException e) {\n                 \/\/\n            } catch (Exception e) {\n                  \/\/ \n            }\n      }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Tukaj je naslednji primer kode:<\/p>\n","protected":false},"author":65283547,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_wpcom_ai_launchpad_first_post":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":false,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1017],"tags":[86835188,207604289],"class_list":["post-247","post","type-post","status-publish","format-standard","hentry","category-java","tag-ustvari-pdf-iz-html","tag-ustvari-pdf-iz-slike"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4CoHV-3Z","jetpack-related-posts":[{"id":194,"url":"https:\/\/vkrastev1blog.com\/2022\/03\/30\/java-configure-log4j2-in-your-project\/","url_meta":{"origin":247,"position":0},"title":"Java konfigurirajte Log4j2 v svojem projektu","author":"vkrastev2014","date":"30.03.2022","format":false,"excerpt":"Log4j2 and SLF4j Binding DependenciesTo make Log4j2 work with SLF4J, we need to include the following 3 dependencies. log4j-slf4j-impl.jar \u2013 Log4j 2 SLF4J binding. It allows applications coded to the SLF4J API to use Log4j2 as the implementation.log4j-api.jar \u2013 provides the adapter components required for implementers to create a logging\u2026","rel":"","context":"In &quot;Java&quot;","block_context":{"text":"Java","link":"https:\/\/vkrastev1blog.com\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":217,"url":"https:\/\/vkrastev1blog.com\/2022\/05\/20\/eclipse-how-to-use-the-jpa-static-metamodel-classes-while-writing-criteria-queries-in-hibernate\/","url_meta":{"origin":247,"position":1},"title":"Eclipse, kako uporabljati razrede stati\u010dnega metamodela JPA med pisanjem poizvedb glede kriterijev v Hibernate","author":"vkrastev2014","date":"20.05.2022","format":false,"excerpt":"When you use Maven you have to add in your POM following dependency: <dependency> <groupId>org.hibernate<\/groupId> <artifactId>hibernate-jpamodelgen<\/artifactId> <version>5.3.7.Final<\/version> <\/dependency> Now you have to add folder target\\generated-sources\\annotations to your classpath as by default, the classes will be generated in this folder only. You go in Project Build Path -> Configure Build Path\u2026","rel":"","context":"In &quot;Java&quot;","block_context":{"text":"Java","link":"https:\/\/vkrastev1blog.com\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":208,"url":"https:\/\/vkrastev1blog.com\/2022\/04\/03\/maven-excluding-jars-from-war-file\/","url_meta":{"origin":247,"position":2},"title":"Maven izklju\u010duje Jars iz datoteke WAR","author":"vkrastev2014","date":"03.04.2022","format":false,"excerpt":"I have had a problem to exclude some Jars from War File with maven. You can do it using maven-war-plugin <project> ... <build> <plugins> <plugin> <artifactId>maven-war-plugin<\/artifactId> <version>3.3.2<\/version> <configuration> <packagingExcludes>WEB-INF\/lib\/xxxx*.jar<\/packagingExcludes> <\/configuration> <\/plugin> <\/plugins> <\/build> ... <\/project>","rel":"","context":"In &quot;Java&quot;","block_context":{"text":"Java","link":"https:\/\/vkrastev1blog.com\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":107,"url":"https:\/\/vkrastev1blog.com\/2014\/05\/20\/important-git-commands\/","url_meta":{"origin":247,"position":3},"title":"Pomembni ukazi in vadnice Git","author":"vkrastev2014","date":"20.05.2014","format":false,"excerpt":"How to get help for a Git commands git help [-a|--all|-i|--info|-m|--man|-w|--web] [COMMAND] Example: git help git It launches a browser and shows a help in HTML. Setting up local repository and making initial push: cd my_project git init git add * git commit -m \"My initial commit message\" git remote\u2026","rel":"","context":"In &quot;Java&quot;","block_context":{"text":"Java","link":"https:\/\/vkrastev1blog.com\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":242,"url":"https:\/\/vkrastev1blog.com\/2023\/12\/13\/html-code-as-iframe-source-rather-than-a-url\/","url_meta":{"origin":247,"position":4},"title":"Koda Html kot vir IFRAME in ne URL","author":"vkrastev2014","date":"13.12.2023","format":false,"excerpt":"You can use srcdoc attribute to include HTML Code to Iframe. <iframe srcdoc=\"<html><body>Hello, <b>world<\/b>.<\/body><\/html>\"><\/iframe>For JSF as htmlCode is the code as String<iframe srcdoc=\"${bean.htmlCode}\"><\/iframe> If your HTML\u00a0contains double quotes, consider\u00a0escaping its double quotes","rel":"","context":"In &quot;Java&quot;","block_context":{"text":"Java","link":"https:\/\/vkrastev1blog.com\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":129,"url":"https:\/\/vkrastev1blog.com\/2014\/05\/26\/new-gpu-bitcoin-miner-is-there-named-diamantminer-how-to-install-on-windows\/","url_meta":{"origin":247,"position":5},"title":"Nov GPU Bitcoin Miner se imenuje DiamantMiner. Kako namestiti v Windows","author":"vkrastev2014","date":"26.05.2014","format":false,"excerpt":"New GPU Bitcoin Miner is there named DiamantMiner. How to install on Windows I have tried the new bitcoin miner and I find it very good. What you should do to install it on Windows is described here. How to build and install DiamantMiner requires the SDK for Java Version\u2026","rel":"","context":"In &quot;drugi&quot;","block_context":{"text":"drugi","link":"https:\/\/vkrastev1blog.com\/category\/drugi\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/vkrastev1blog.com\/wp-json\/wp\/v2\/posts\/247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vkrastev1blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vkrastev1blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vkrastev1blog.com\/wp-json\/wp\/v2\/users\/65283547"}],"replies":[{"embeddable":true,"href":"https:\/\/vkrastev1blog.com\/wp-json\/wp\/v2\/comments?post=247"}],"version-history":[{"count":7,"href":"https:\/\/vkrastev1blog.com\/wp-json\/wp\/v2\/posts\/247\/revisions"}],"predecessor-version":[{"id":254,"href":"https:\/\/vkrastev1blog.com\/wp-json\/wp\/v2\/posts\/247\/revisions\/254"}],"wp:attachment":[{"href":"https:\/\/vkrastev1blog.com\/wp-json\/wp\/v2\/media?parent=247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vkrastev1blog.com\/wp-json\/wp\/v2\/categories?post=247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vkrastev1blog.com\/wp-json\/wp\/v2\/tags?post=247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}