Insert attachment in script (vf page)
<apex:page action="{!callMethod}" showHeader="false" controller="ConvertInWordController">
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js'></script>
<script src="/soap/ajax/51.0/connection.js" type="text/javascript"></script>
<script type="text/javascript">
function Export2Doc(element, filename = '') {
alert('call 2 '+ location.href);
var meta = "Mime-Version: 1.0\nContent-Base: " + location.href + "\nContent-Type: Multipart/related; boundary=\"NEXT.ITEM-BOUNDARY\";type=\"text/html\"\n\n--NEXT.ITEM-BOUNDARY\nContent-Type: text/html; charset=\"utf-8\"\nContent-Location: " + location.href + "\n\n<!DOCTYPE html>\n<html>\n_html_</html>";
var head = "<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n<style>\n_styles_\n</style>\n</head>\n";
var html = document.getElementById(element).innerHTML;
console.log('html : '+ html);
//alert(html);
var blob = new Blob(['\ufeff', html], {
type: 'application/msword'
});
var css = (
'<style>' +
'img {width:300px;}table {border-collapse: collapse; border-spacing: 0;}td{padding: 6px;}' +
'</style>'
);
var options = { maxWidth: 624 };
var images = Array();
var img = $("#" + element).find("img");
for (var i = 0; i < img.length; i++) {
var w = Math.min(img[i].width, options.maxWidth);
var h = img[i].height * (w / img[i].width);
var canvas = document.createElement("CANVAS");
canvas.width = w;
canvas.height = h;
var context = canvas.getContext('2d');
context.drawImage(img[i], 0, 0, w, h);
var uri = canvas.toDataURL("image/png");
$(img[i]).attr("src", img[i].src);
img[i].width = w;
img[i].height = h;
images[i] = {
type: uri.substring(uri.indexOf(":") + 1, uri.indexOf(";")),
encoding: uri.substring(uri.indexOf(";") + 1, uri.indexOf(",")),
location: $(img[i]).attr("src"),
data: uri.substring(uri.indexOf(",") + 1)
};
}
var imgMetaData = "\n";
for (var i = 0; i < images.length; i++) {
imgMetaData += "--NEXT.ITEM-BOUNDARY\n";
imgMetaData += "Content-Location: " + images[i].location + "\n";
imgMetaData += "Content-Type: " + images[i].type + "\n";
imgMetaData += "Content-Transfer-Encoding: " + images[i].encoding + "\n\n";
imgMetaData += images[i].data + "\n\n";
}
imgMetaData += "--NEXT.ITEM-BOUNDARY--";
var output = meta.replace("_html_", head.replace("_styles_", css) + html) + imgMetaData;
console.log('@@@@@@ : '+ output);
//var url = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(output);
filename = filename ? filename + '.doc' : 'document.doc';
sforce.connection.sessionId =
'{!$Api.Session_ID}'
;
var contentVersion = new sforce.SObject("ContentVersion");
contentVersion.Title = 'test';//filename || 'Untitled Document';
contentVersion.VersionData = btoa(output);
contentVersion.PathOnClient = 'Test123.doc';
contentVersion.FirstPublishLocationId = '0065h00000P010pAAB';
var result = sforce.connection.create([contentVersion]);
if (result[0].success == 'true') {
//alert('Document saved successfully in Salesforce!');
} else {
//alert('Error saving document: ' + result[0].errors.message);
}
}
window.onload = function () {
console.log('@@@');
Export2Doc('container', 'document');
};
</script>
<script>
console.log('call');
alert('call');
</script>
<!-- Call the JavaScript function directly -->
<script>
console.log('Calling Export2Doc');
exportDocument();
</script>
</head>
<body>
<apex:form>
<apex:actionFunction name="exportDocument" action="{!callMethod}" rerender="none"/>
</apex:form>
<div id="container">
<p>
ravi jangid
</p>
<apex:outputText value="{!document}"></apex:outputText>
</div>
</body>
</html>
</apex:page>
Comments
Post a Comment