-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Creating PDF from HTML #34
Comments
API (and the internal code) for this plugin is still in development, so, apologies for the rough edges. In this case, "BETA" (as mentioned in the plugin header) really means that - wrong step and it will misbehave. Take a look at /examples. Look for 'fromHTML' inside 'basic.js' What you might be missing is the "width" parameter, which, at this point, is required for internal logic. There are a few fixes (which I did not yet push upstream) for this module here https://github.com/willowsystems/jsPDF/tree/master Again, this plugin is Alpha-ish Beta, so be patient with it. :) |
Thank you. |
dvdotsenko, noticed your working on this... couple things I had issue with. Couple things were undefined and causing an issue. Not sure if I fixed them in the right way, but it worked. css_font_family_string if (typeof css_font_family_string == "undefined") {
css_font_family_string = ""
} css_line_height_string if (typeof css_line_height_string != "undefined") {
value = css_line_height_string.match( /([\d\.]+)(px)/ )
if (value.length === 3) {
// caching, returning
return UnitedNumberMap[css_line_height_string] = parseFloat( value[1] ) / 16.00
}
} |
The code changes are against which repo/branch? Not everything done by me is pushed to upstream (MrRio's) repo. (Mostly because this pool request, rebase on top of MrRio's merge commit routine is annoying for small, non-urgent commits. Partly because some of the code, especially from_html, is still ugly.) The link I posted above is to "my" jsPDF repo (https://github.com/willowsystems/jsPDF) and I could not find the offending lines in there. I indeed had some issues with IE giving me undefined values for things that Chrome and others calc the values for. So, it's possible the items you had issue with are already paved over in my repo. Try. I welcome feedback. |
It was from your repo.. the offending lines were: , parts = css_font_family_string.split(',') // yes, we don't care about , inside quotes and value = css_line_height_string.match( /([\d\.]+)(px)/ ) They didn't check for undefined, so it gave me an error when it got to them. So I just put in the check to essentially skip it if they were undefined, which is what the code above was. Sorry for the confusion. |
I know that the from_html plugin is in active development, but I've implemented a custom renderer for the html2canvas library that translates all of the canvas instructions it generates into instructions for jspdf + addimage plugin. repo is https://github.com/jcook-logixml/html2canvas/ and renderer is https://github.com/jcook-logixml/html2canvas/blob/master/src/renderers/PDF.js |
Yeah. html2canvas is very cool. I was looking at it for a long time, thinking about just adopting it, but found the "canvas" side inflexible. canvas API has no notion of paragraphs, line spacing, etc. There is definitely no way to inject paging. There is no (obvious to me) way to inject custom handlers selected DOM element blocks. For example, we do contract conversion from browser screen to PDF. Part of the presentation is http://willowsystems.github.com/jSignature/ which is already a chunk of canvas. I don't want it to be drawn as shown. I want the signature data to be exported as SVG and inserted as clean vectors into resulting PDF = custom rendering handler for an inner block of the page. Custom handlers, paging, font and paragraph control are hard to finetune with html2canvas, as all you get is a "photo" of the page, not its semantic textual representation that is refitted to size of the PDF pages. So, it is with this profound sadness in my heart i must say that I personally don't see a bright future for "canvas" somewhere in between "html" and "pdf." I am truly sad about this realization as it means I need to write more code and maintain something like fromHTML jsPDF plugin. |
Do I need to have any eventHandlers defined or does the plugin take care of them by default? It won't let me run it without eventHandlers and if I use the one in your example to keep it from being undefined, I don't get any PDF output to screen, just a white page. P.S. this is using the from_HTML plugin. |
The simplest example of from_html implementation is in examples folder. Load examples/basic.html and look at the "text" tab for example. https://github.com/willowsystems/jsPDF/blob/stable/examples/js/basic.js#L310 The code should work just fine without
|
I've been trying for the majority of this weekend to get the from_html plugin to work. Unfortunately, each time I try to call the plugin, I get some sort of a null-related error. I fixed a few, but more popup down the line. So, I am trying to start over from the beginning. The initial error I receive is "Cannot call method 'appendTo' of null" This is originating in the process() function of the from_html code. Has anyone had this issue? Am I missing something dumb? I'm working in a cloud platform that has the .min file for jQuery v1.4.4 and NoConflict mode is enabled. *****NOTE: I have just now been able to circumnavigate this issue. I was forced to declare and initialize variables that are found in the process() function. I'm still not sure why I have to, though. I'm also not sure if this approach will hurt me on down the line somewhere. Here's how the guts of my process() function now looks:
|
The problem you were having originally is the "noConflict" part. fromHTML assumes global $ is jQuery, while in your case I don't know what it is, Moo? Prototype? Although https://github.com/willowsystems/jsPDF/blob/stable/jspdf.plugin.from_html.js#L537 It uses jQuery-specific ability to materialize a DOM fragment from a string of HTML. All other toolkits likely don't know what to do with it and just silently continue without creating the actual iframe. There is nothing technically wrong with my version of Process(), but the whole of jsPDF, or just the fromHTML piece would need to be wrapped into a closure that captures jQuery as $:
I also apologize for my unusual coding style (no semicolons, comma first, one var on top). It helps me tremendously to avoid mistakes, but I see how it confused the crap out of you. |
No apologies necessary! The formatting didn't confuse me, but it's certainly not what I'm used to viewing. I had to change it for my own sanity. You're absolutely right. That is the line that caused my issues. I kept trying to work around it with not much luck at first. What you've pointed out makes absolute sense to me. We use Jelly, so after what you said, I'm quite sure that is the default $. Being fairly new to the client-side of things, I was unaware of that. Thanks very much for your tips. |
Trying to use the fromHTML plug-in and take control over formatting from the DOM elements passed in via the initial call. I thought hooking in an elementHandler would do it but the code is never getting reached. For example:
The alert never is reached so I can override the formatting via other calls to the APIs. All LI elements are processed via the default behavior. Is what I am trying to do not supported or am I just botching this up? UPDATE: Looks like you can only do it based on IDs. I change 'li' above to '#test_id' and the code fires. Would be nice if elements or at least classes could be used as selectors (I know this is very early beta). |
https://github.com/MrRio/jsPDF/blob/master/jspdf.plugin.from_html.js#L458 :
Because the matching is done against every element in the node tree, my desire was to make it as fast as possible. In that case, it meant "Only element IDs are matched" The element IDs are still done in jQuery style "#id", but it does not mean that all jQuery selectors are supported. There is still room for full support of jQuery selectors, but then instead of doing a simple object property look up, the code would need to loop over all special handler selector strings and see if any one matches the elem. So, instead of that one simple line above, we would need something like this:
I certainly don't mind having full jQuery selector functionality, but am currently preferring speed for flexibility. I would not want to have a switch or option for this, as it just complicates the things. I guess what I would prefer if someone finds the most efficient logic for finding a matching selector to an elem. If the overall parsing code's speed is hindered by only some 2-5% I would not see this as a problem. Daniel. |
Hi, I have a program in .NET 1.1 and I'm trying use the example HTML Renderer, but in IE8 show the error, in javascript debug, 'fof' is null or is not a object. Reference line 48 in jspdf.plugin.split_text_to_size.js: widthsFractionOf = widths.fof ? widths.fof : 1. My page aspx have only this:
and this is my javascript:
If I delete the tag < h1 > it's work. But in my real case I have much more tags HTML. This same code works for IE9, but I need that's works IE9 and below. |
how to create dynamic table using jspdf ????. |
Hi there I'm pretty new to jsPDF, but after some digging I was wondering if there were any ways to get content ( in my case HTML or SVG ) to PDF WITHOUT rastersizing it ( aka preserve text selection as well as scalable graphics within it ). From what I get by now, the "rastersizing" comes from using in the plugins used by jsPDF ? Among other stuff, I tried "printing" to a PDF file using Chrome & the resulting PDF ( whether it contains divs with svg backgrounds or embed svgs ) has the scalability & text selections preserved, as well as the PDF "pages" have the correct dimensions ( set using css @print & @page rules ) I still have to try using the "html with background svg" version with jsPDF, hoping it will preserve the vector aspect of the div background as well as print it on the resulting doc In case anyone is interested, some magic seems to happen here ( I'm currently digging it ;p ) Moreover, I'd be happy to write some plugin if someone had a working solution :P This being said, thanks for the nice js lib ;) Cheers + |
@stephaneAG |
Is there a reason why This is a great thread though, and thank for jsPDF! This explains it: issue #516 (comment)
|
I am trying to create pdf from html using form_html plugin. But what is "setting" argument in fromHTML API. i got some error when using the below code.
MY actual code is like this.
The text was updated successfully, but these errors were encountered: