Giter Site home page Giter Site logo

wkhtmltopdf-crystal's Introduction

WkHtmlToPdf for Crystal - PROJECT UNMAINTAINED Build Status

This project is not maintained anymore

If you like it or continue to use it fork it please.



Crystal wrapper for libwkhtmltox C library.

wkhtmltopdf and wkhtmltoimage permit to render HTML into PDF and various image formats using the Qt WebKit rendering engine - see wkhtmltopdf.org

Requirements

  • libwkhtmltox must be installed
  • pkg-config must be available

Installation

  • Add this to your application's shard.yml:
dependencies:
  wkhtmltopdf-crystal:
    github: blocknotes/wkhtmltopdf-crystal
  • If wkhtmltox library is installed but missing for Crystal compiler: copy wkhtmltox.pc (from lib/wkhtmltopdf-crystal folder) in a pkg-config folder (ex. /usr/local/lib/pkgconfig) or set the environment variable PKG_CONFIG_PATH with the path to wkhtmltox.pc before compiling
  • Optinally edit wkhtmltox.pc with the correct path to wkhtmltox (default headers path: /usr/local/include/wkhtmltox)

Usage

HTML to PDF:

require "wkhtmltopdf-crystal"
Wkhtmltopdf::WkPdf.new( "test.pdf" ).convert( "<h3>Just a test</h3>" )

Fetch URL content and convert it to JPG:

require "wkhtmltopdf-crystal"
img = Wkhtmltopdf::WkImage.new
img.set_url "http://www.google.com"
img.set_output "test.jpg"
img.set "quality", "90"
img.convert

Write to buffer (only if no output is specified):

require "wkhtmltopdf-crystal"
pdf = Wkhtmltopdf::WkPdf.new
pdf.convert "<h3>Just a test</h3>"
pdf.object_setting "footer.right", "[page] / [topage]" # Set page counter on footer
unless pdf.buffer.nil?
  puts "PDF buffer size: " + pdf.buffer.try( &.size ).to_s
end

Lib settings (available with set / object_setting methods on wrappers): libwkhtmltox pagesettings

More examples

See examples folder. Includes a Kemal example to print an ECR view in PDF.

Troubleshooting

Invalid memory access

  • If this component needs to be called multiple times it's necessary to initialize the library in the constructor and call deinitialize when all is done. Example:
require "wkhtmltopdf-crystal"
pdf = Wkhtmltopdf::WkPdf.new "", true
at_exit do
  pdf.deinitialize
end

pdf.set_output "test1.pdf"
pdf.convert "<h3>Just a test 1</h3>"

pdf.set_output "test2.pdf"
pdf.convert "<h3>Just a test 2</h3>"

Contributors

wkhtmltopdf-crystal's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

wkhtmltopdf-crystal's Issues

Invalid memory access (signal 11) at address 0x20

Hi!
I use kemal.
I have some code

post "/snapshot" do |env|
  env.response.content_type = "application/json"
  url = env.params.body["url"].as(String)
  url = URI.unescape(url)

  filename = "#{Time.new.epoch}-image.jpg"
  filepath = ::File.join ["/storage", filename]

  img = Wkhtmltopdf::WkImage.new
  img.set_url url
  img.set_output filepath
  img.set "fmt", "jpg"
  img.set "quality", "70"
  img.set "screenWidth", "1088"
  img.set "screenHeight", "680"
  img.convert

  {images: [{url: filepath}]}.to_json
end

After first request to /snapshot i have success result and path image.
But after second request i see error:

QObject::connect: Cannot connect (null)::configurationAdded(QNetworkConfiguration) to QNetworkConfigurationManager::configurationAdded(QNetworkConfiguration)
QObject::connect: Cannot connect (null)::configurationRemoved(QNetworkConfiguration) to QNetworkConfigurationManager::configurationRemoved(QNetworkConfiguration)
QObject::connect: Cannot connect (null)::configurationChanged(QNetworkConfiguration) to QNetworkConfigurationManager::configurationChanged(QNetworkConfiguration)
QObject::connect: Cannot connect (null)::onlineStateChanged(bool) to QNetworkConfigurationManager::onlineStateChanged(bool)
QObject::connect: Cannot connect (null)::configurationUpdateComplete() to QNetworkConfigurationManager::updateCompleted()
Invalid memory access (signal 11) at address 0x20
[4337692939] *CallStack::print_backtrace:Int32 +107
[4337624055] __crystal_sigfault_handler +55
[140736045681594] _sigtramp +26
[4369833558] _ZN35QNetworkConfigurationManagerPrivate13enablePollingEv +22
[4369809925] _ZN28QNetworkConfigurationManagerC2EP7QObject +165
[4369607424] _ZN21QNetworkAccessManager13createRequestENS_9OperationERK15QNetworkRequestP9QIODevice +640
[4345328155] _ZN11wkhtmltopdf22MyNetworkAccessManager13createRequestEN21QNetworkAccessManager9OperationERK15QNetworkRequestP9QIODevice +1963
[4369601041] _ZN21QNetworkAccessManager3getERK15QNetworkRequest +33
[4354134631] _ZN7WebCore20QNetworkReplyHandler18sendNetworkRequestEP21QNetworkAccessManagerRKNS_15ResourceRequestE +439
[4354131783] _ZN7WebCore20QNetworkReplyHandler5startEv +71
[4354120890] _ZN7WebCore29QNetworkReplyHandlerCallQueue5flushEv +186
[4354131487] _ZN7WebCore20QNetworkReplyHandlerC2EPNS_14ResourceHandleENS0_8LoadTypeEb +463
[4354111805] _ZN7WebCore14ResourceHandle5startEPNS_17NetworkingContextE +365
[4352743522] _ZN7WebCore14ResourceHandle6createEPNS_17NetworkingContextERKNS_15ResourceRequestEPNS_20ResourceHandleClientEbb +178
[4351797971] _ZN7WebCore18MainResourceLoader7loadNowERNS_15ResourceRequestE +403
[4351798657] _ZN7WebCore18MainResourceLoader4loadERKNS_15ResourceRequestE[4351702197] _ZN7WebCore11FrameLoader37callContinueLoadAfterNavigationPolicyEPvRKNS_15ResourceRequestEN3WTF10PassRefPtrINS_9FormStateEEEb +37
[4351811813] _ZN7WebCore14PolicyCallback4callEb +69
[4351816489] _ZN7WebCore13PolicyChecker29continueAfterNavigationPolicyENS_12PolicyActionE +505
[4360504075] _ZN7WebCore19FrameLoaderClientQt39dispatchDecidePolicyForNavigationActionEMNS_13PolicyCheckerEFvNS_12PolicyActionEERKNS_16NavigationActionERKNS_15ResourceRequestEN3WTF10PassRefPtrINS_9FormStateEEE +203
[4351814749] _ZN7WebCore13PolicyChecker21checkNavigationPolicyERKNS_15ResourceRequestEPNS_14DocumentLoaderEN3WTF10PassRefPtrINS_9FormStateEEEPFvPvS3_S9_bESA_ +1805
[4351698647] _ZN7WebCore11FrameLoader22loadWithDocumentLoaderEPNS_14DocumentLoaderENS_13FrameLoadTypeEN3WTF10PassRefPtrINS_9FormStateEEE +1111
[4351696213] _ZN7WebCore11FrameLoader4loadEPNS_14DocumentLoaderE +197
[4351695679] _ZN7WebCore11FrameLoader4loadERKNS_15ResourceRequestERKNS_14SubstituteDataEb +351
[4351695260] _ZN7WebCore11FrameLoader4loadERKNS_15ResourceRequestEb +60
[4360219122] _ZN9QWebFrame4loadERK15QNetworkRequestN21QNetworkAccessManager9OperationERK10QByteArray +1570
[4345343485] _ZN11wkhtmltopdf14ResourceObject4loadEv +2685
[4345347650] _ZN11wkhtmltopdf22MultiPageLoaderPrivate4loadEv +98
[4345485447] _ZN11wkhtmltopdf21ImageConverterPrivate12beginConvertEv +199
[4345364849] _ZN11wkhtmltopdf9Converter7convertEv +33
[4345498813] wkhtmltoimage_convert +13
[4339121093] *Wkhtmltopdf::WkImage#convert<Nil>:(Int32 | Nil) +325
[4339120741] *Wkhtmltopdf::WkImage#convert:(Int32 | Nil) +21
[4337656178] ~procProc(Nil)@src/channelkit-parser.cr:119 +386
[4337715780] *Fiber#run:(IO::FileDescriptor | Nil) +164

Missing images in pdf file

I am converting a markdown document to html, then to pdf. The document has images referenced in it which show up in the html conversion. The conversion to pdf renders little icons where the images should be. Is this normal behavior for wkhtmltopdf-crystal? Can the images be rendered in the resulting pdf?

require "./pdfgen/*"
require "wkhtmltopdf-crystal"

html = File.read("./README.html")
pdf = Wkhtmltopdf::WkPdf.new
pdf.set_output "html_to_pdf.pdf"
pdf.convert html

I tried the command line wkhtmltopdf and it worked as advertised. What am I missing?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.