Is there any option available to set all unit as px in css while converting pdf to html in HtmlSaveOptions.
@rupabind
To set the units for CSS in HTML conversion using the HtmlSaveOptions
class. You can specify the CustomCssSavingStrategy
property to control the units in the generated CSS. However, there isn’t a direct option to set all units to “px” in the CSS using this property.
You can achieve this by using a custom CSS file where you define the units explicitly as “px” for each relevant style property, and then reference this CSS file during the HTML to PDF conversion.
Here’s an example of how you can do this:
using Aspose.Pdf;
// Load the PDF document
Document pdfDocument = new Document("input.pdf");
// Initialize HtmlSaveOptions
HtmlSaveOptions htmlSaveOptions = new HtmlSaveOptions
{
CustomCssSavingStrategy = new CssSavingStrategy(),
HtmlMarkupGenerationMode = HtmlMarkupGenerationModes.Html5
};
// Specify the path to your custom CSS file
htmlSaveOptions.CustomCssUri = "path/to/custom.css";
// Convert PDF to HTML
pdfDocument.Save("output.html", htmlSaveOptions);
In this example:
- Load the PDF document.
- Initialize the
HtmlSaveOptions
, set theCustomCssSavingStrategy
to use a custom CSS file, and specify theHtmlMarkupGenerationMode
as needed. - Specify the path to your custom CSS file using the
CustomCssUri
property. In your custom CSS file, you can define units as “px” for relevant styles.
Here’s an example of what your custom CSS file (custom.css
) might look like:
/* Define units as px for relevant styles */
body {
font-size: 14px;
}
p {
margin: 10px 0;
font-size: 16px;
}
/* Add more CSS rules as needed */
By creating a custom CSS file and specifying it in the CustomCssUri property, you can control the units used in the generated HTML/CSS during the PDF to HTML conversion process. Please make sure to adjust the CSS rules in the custom file to match your specific styling requirements.
@sergei.shibanov
Thank you for the response.
I am looking for PDF to HTML conversion instead of the other way round.
The CSS file is getting generated from the Aspose library and the generated CSS has the units as EM and PX .But my requirement is that all the units should be in PX .Do we have any property which we can set while converting pdf into html which will set all these units in PX .
Also I am facing below mentioned issues with the code provided:
1) Unable to set CustomCssSavingStrategy property.
2) Getting 'HtmlSaveOptions' does not contain a definition for 'CustomCssUri' while setting
CustomCssUri property in below statement
htmlSaveOptions.CustomCssUri = "path/to/custom.css";
Can you clarify which version of library is being used because when i am adding the above line in my code it is giving me error .(I am referring the latest version,23.9.0)
3)When i am trying to set CustomCssSavingStrategy property i am getting this error
**System.ApplicationException**: 'Inconsistent saving options detected:
'CustomStrategyOfCssUrlCreation' and 'CustomCssSavingStrategy' must be both set or both
unset!'
@rupabind
I provide more complete code, as I understand your request.
using Aspose.Pdf;
using static Aspose.Pdf.HtmlSaveOptions;
namespace PdfToHtml
{
internal class Program
{
static void Main(string[] args)
{
// Load the PDF document
var pdfDocument = new Document("input.pdf");
// Initialize HtmlSaveOptions
var htmlSaveOptions = new HtmlSaveOptions
{
CustomCssSavingStrategy = new CssSavingStrategy(CssSavingStrategy),
HtmlMarkupGenerationMode = HtmlMarkupGenerationModes.WriteAllHtml
};
// Specify the path to your custom CSS file
htmlSaveOptions.CustomStrategyOfCssUrlCreation = new HtmlSaveOptions.CssUrlMakingStrategy(Strategy_MAKING_CUSTOM_URL);
// Convert PDF to HTML
pdfDocument.Save("output.html", htmlSaveOptions);
}
private static void CssSavingStrategy(HtmlSaveOptions.CssSavingInfo partSavingInfo)
{
}
private static string Strategy_MAKING_CUSTOM_URL(HtmlSaveOptions.CssUrlRequestInfo requestInfo)
{
return "path/to/custom.css";
}
}
}
I got the property name wrong - I apologize.
Thank you for your reply.
You mentioned custom.css in the code, but I can’t acquire the css file before html conversion, and I need to create html for distinct pdfs, thus separate html and css will be generated for each pdf.I’m thinking about how I’m going to make custom.css for various pdfs.Also, when I execute your code, it uses the same custom.css but does not modify it as per the css should be genreted for the html.
When i am using CustomCssSavingStrategy and CustomStrategyOfCssUrlCreation then style.css file not getting generated.Is there any option to get the sylte.css file along with providing the custom.css file.
@rupabind
Now there is no way to simply set the option in HtmlSaveOptions to get all unit as px in css.