I have this form
where with oleie control i load a custom page that user has to compile...In the image you can see only a part of the document that is longer than what is shown..... Once the user has compiled this document is there a way to save this document as pdf file ?
Thanks
Ciao Michele,
if I understand correctly, you display a HTML page where the user fills in the fields.
FoxInCloud renders this HTML page inside an iframe
, printing the contents of this iframe
(document.innerHTML
) into PDF is, as far as I know, very much dependent on the client browser and maybe not supported on all.
You may want to test Print.js on the client side.
You can also send the HTML contents to the server for conversion to PDF (not sure what library you can use):
procedure cmd.click
lparameters tcHTML
do case
case thisForm.wlHTMLgen && HTML generation
local cJS
text to cJS textmerge noshow flags 1 pretext 3
var html = jQuery('#<<this.parent.oleIE.wcID>>').prop('contentDocument').documentElement.innerHTML
return jQuery.proxy(FoxInCloud.DOMEvent, FoxInCloud, html)()
endtext
return m.cJS
case vartype(m.tcHTML) == 'C' and !empty(m.tcHTML) && Web mode
local cPDF
cPDF = addBS(thisForm.woConfig.cTempPath) + 'myForm.pdf'
if strtofile(htmlToPdf(m.tcHTML), m.cPDF) > 0 && htmlToPdf() to be replaced by your conversion library
return thisForm.wFileSaveAs(m.cPDF)
else && PDF generation or write error
thisForm.wMessageBox('error')
endif
otherwise && desktop mode
endcase
I don't understand where i have to insert the code you suggested and how to call it and how to pass the parameter you set tcHTML Thanks
procedure cmd.click
is for a "save as PDF" VFP command button to be added to the form. All code goes in this procedure. FoxInCloud populates tcHTML
automatically.
PDFcrowd API seems to be a good candidate for server-side HTML to PDF conversion.
I inserted your code in the click event of a button... but when i click the button nothing is done...
did you check the usual debugging sources: JavaScript errors and network XHR responses?
jquery-migrate.min.js?2.31.0-beta.4:2 Uncaught TypeError: Cannot read property 'type' of undefined
at Object.a.event.fix (jquery-migrate.min.js?2.31.0-beta.4:2)
at Object.DOMEvent (FoxInCloud.js?2.31.0-beta.4:89)
at i (jquery.min.js?2.31.0-beta.4:2)
at HTMLButtonElement.eval (eval at DOMreadyUpdate (FoxInCloud.js?2.31.0-beta.4:1442), <anonymous>:32:59)
at HTMLButtonElement.dispatch (jquery.min.js?2.31.0-beta.4:2)
at HTMLButtonElement.v.handle (jquery.min.js?2.31.0-beta.4:2)
a.event.fix @ jquery-migrate.min.js?2.31.0-beta.4:2
DOMEvent @ FoxInCloud.js?2.31.0-beta.4:89
i @ jquery.min.js?2.31.0-beta.4:2
eval @ VM113:32
dispatch @ jquery.min.js?2.31.0-beta.4:2
v.handle @ jquery.min.js?2.31.0-beta.4:2
Please try this fix:
case thisForm.wlHTMLgen && HTML generation
local cJS
text to cJS textmerge noshow flags 1 pretext 3
var html = jQuery('#<<this.parent.oleIE.wcID>>').prop('contentDocument').documentElement.innerHTML
return FoxInCloud.DOMEvent(html, undefined, event)
endtext
return m.cJS
Now i can save the html, but in the html there aren't what i compiled in th page...It is empty
Have you checked if the HTML sent by the browser is empty?
It is not empty, but the values i inserted in the textboxes are empty. In the example i sent a t the begin of this thread the value inserted for the label "Codice catasto" is empty and i can see the inserted text... This is my click event code
Lparameters tcHTML
If Thisform.wlHTMLgen
local cJS
text to cJS textmerge noshow flags 1 pretext 3
var html = jQuery('#<<this.parent._oleIE1.wcID>>').prop('contentDocument').documentElement.innerHTML
return FoxInCloud.DOMEvent(html, undefined, event)
EndText
Return m.cJS
Endif
If Vartype(m.tcHTML) == 'C' And !Empty(m.tcHTML)
=StrToFile(m.tcHTML,"aa.Html")
Endif
The file aa.html is created, but if i open it all textboxes are empty
You can try to write the value into the value
attribute
local cJS
text to cJS textmerge noshow flags 1 pretext 3
var $doc = jQuery(jQuery('#<<this.parent._oleIE1.wcID>>').prop('contentDocument').documentElement)
$doc.find('input').each(function(idx,inp){
var $inp = jQuery(inp);
$inp.attr('value', $inp.val());
});
return FoxInCloud.DOMEvent($doc.html(), undefined, event)
EndText
Return m.cJS
It works. Great job. Thanks Now i'll try to add the part to convert the html in pdf Do you know a control that let to convert html in pdf using foxpro ? The one you suggested asks to use java not fox... and with this i'll have more support from you
know a control to convert html in pdf using foxpro
I don't know any, reason why I suggested an API earlier
I created a demo account in https://pdfcrowd.com/doc/api/html-to-pdf/http/
you can try:
If Vartype(m.tcHTML) == 'C' And !Empty(m.tcHTML)
* convert to PDF using https://pdfcrowd.com/doc/api/html-to-pdf/http/
local oHTTP, cPDF, cPDFfile
oHTTP = createObject('wwHTTP')
oHTTP.addPostKey('text', m.tcHTML)
oHTTP.addPostKey('page_size', 'A4') && see the other options in the above URL
cPDF = oHTTP.HTTPget('https://api.pdfcrowd.com/convert/', 'FoxInCloud', '082ea01f318b31574160441b70f589b3')
cPDFfile = …
strtofile(m.cPDF, m.cPDFfile)
thisForm.wFileSaveAs(m.cPDFfile)
endif
I created an account to try it, but
I receive an error message when loading the pdf document
"Impossible to load the pdf document"
I'll check it...
This works for me:
local oHTTP as wwHTTP of wwHTTP.prg, cPDF, cPDFfile
oHTTP = createObject('wwHTTP')
oHTTP.addHeader('Authorization', 'Basic ' + Strconv('FoxInCloud:082ea01f318b31574160441b70f589b3', 13)) && use your account credentials
oHTTP.addPostKey('text', m.tcHTML)
oHTTP.addPostKey('page_size', 'A4') && see the other options available
cPDF = oHTTP.HTTPget('https://api.pdfcrowd.com/convert/')
? cTronc(m.cPDF, 200)
cPDFfile = ForceExt(cModuleInfo(Sys(16), 'A', .T.), 'pdf')
? Strtofile(m.cPDF, m.cPDFfile) > 0
The only issue is how to make the CSS available to pdfcrowd despite relative URLs; adding a <base>
tag to the page should be OK.
Now it works. Great! Thanks for support Thierry.
Thanks
Just to see the result, can you share how look the result PDF and source HTML form?
Here is the image of my html in my web app
and here is the pdf
Some little differences in font, but data are correct and this was our target
Yes, looks fine
The question mark means an encoding mismatch; PDFcrowd must have an option for that…
For checkboxes you may need:
$doc.find('input').each(function(idx,inp){
var $inp = jQuery(inp);
$inp.attr('value', $inp.val());
}).filter('[type=checkbox]').each(function(idx,chk){
var $chk = jQuery(chk);
if ($chk.prop('checked')) $chk.attr('checked', '');
});
I checked the content of tcHTML variable and if i make filetostr() of this variable in a html file. The html file has already problems with special characters, before to convert it in pdf format.... I am not sure this is a problem of the converter
I guess PDFcrowd
expects HTML encoded in utf-8
, standard all over the Web
The encoding of tcHTML
should be the same as your original HTML form.
If your form is encoded as windows-1252
(VFP default), you need to convert it into utf-8
when sending to PDFcrowd
:
oHTTP.addPostKey('text', cUTF8(m.tcHTML))
When in doubt about a file's encoding, I use Sublime Text's reopen with encoding
feature.
In my form i show a1.html file. In my "Save As PDF" button i receive as parameter tcHTML I make a StrToFile(tcHtml,"a2.Html") before to convert the file in pdf If i open A1.html file i have no problems, if i open a2.html all special characters are not shown If i make a file compare the only differences are:
<!doctype html>
<html lang="en">
at the beginning of the original file and value="" in all input element in a2.html
I inserted the code you suggested but without success
please share a1.html
and a2.html
sublime text detects:
- a1.html:
UTF-8
(while it has<meta charset="Western (Windows 1252)">
) - a2.html:
Western (Windows 1252)
I would try:
oHTTP.addPostKey('text', cUTF8(StrTran(m.tcHTML, '<meta charset="Western (Windows 1252)">')))
You may need several tests and/or PDFcrowd support to figure out the right encoding.
If you use Sublime Text, make sure to add to your user preferences:
…
// Display file encoding in the status bar
"show_encoding": true,
…